Hidetoshi NAGAI
4/15/2008 1:45:00 AM
From: 7stud -- <bbxx789_05ss@yahoo.com>
Subject: Re: ruby/tk and loop delay
Date: Mon, 14 Apr 2008 19:22:40 +0900
Message-ID: <3e5b63766333f2168454e9f0f88479db@ruby-forum.com>
> Isn't after() supposed to accomplish the same thing? Why doesn't
> after() work in my code in my previous post?
Probably, you misunderstand about 'update'.
It is the command to force to flush the event queue.
First, 'update' executes each of the standard evenst (window-event,
timer-event, file-event, and so on) on the event queue.
Next, 'update' executes each of the idle tasks on the queue.
After those (the event queue is empty), 'update' finishes and returns.
'update_idletasks' doesn't executes standard events.
It executes idle tasks only.
'update' and 'update_idletasks' are used to avoid the trouble
about the conflict between current status and planned status
of a widget.
Each widget, when needs re-drawing, registers an idle task
(for re-drawing) to the event queue.
Idle tasks are called by the eventloop, when no standard event
exists on the event queue.
So,
> 10.times do |i|
> x += 10
> TkcLine.new(widget, x, y, x+100, y+100)
>
> t = Thread.new do
> sleep(2)
> widget.update
> end
>
> end
you cannot delay drawing widgets by 'update' or 'update_idletasks'.
Even if you replace 'widget.update' to 'TkcLine.new(...)',
your code will write 10 lines at much the same time,
because those 10 threads starts at much the same time.
If you need 2 seconds of intervel, you must set the next callback
in each callback of after().
TkTimer objects do that. For example,
---------------------------------------------------------------------
TkTimer.new(2000, 10){|tm|
x, y = tm.return_value # <= return value of the previous callback
x += 10
TkcLine.new(widget, x, y, x+100, y+100)
[x, y] # => passed to the next callback
}.start{ [50, 50] } # <= initail proc: return value is passed to the
# first callback
---------------------------------------------------------------------
However, the intervals are not accurate.
Tk doesn't have RealTime monitor, so the time of callback execution
and the timing of starting callbacks are the reason of why.
TkRTTimer objects do error correction about intervals.
The intervals are not accurate, too.
But TkRTTimer objects keep difference between summation of the
intervals and realtime small.
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)