On Thursday, March 14, 2019 at 6:34:18 AM UTC-7, Ashok wrote:
> (The [after $ticks] does not cause the event loop to run.)
I've always wondered about how to best write a sleep routine.
Here's what I use, as otherwise using [after ms] does not let
me see any [puts] debugging statements to appear unless I were
to do an [update]. And update seems to always be frowned upon.
So, any ideas? Here's what I've come up with.
proc sleepms { ms } {
set uniq [ clock clicks ]
set ::__sleep__tmp__$uniq 0
after $ms set ::__sleep__tmp__$uniq 1
vwait ::__sleep__tmp__$uniq
unset ::__sleep__tmp__$uniq
}
The reason for the variable uniq is so that if while I
am waiting, an event (e.g. a button push in a window)
were to happen and the -command code also included a call
to this [sleepms], then it would not clobber the earlier
call by using a unique global variable name.
However, there is a problem here. If a second [sleepms]
does occur in an event, and the sleep time is longer than
the original sleep time, then it will end up waiting until
the event driven wait has finished. But at least I can get
my puts to work (on my windows system).
For example, if after starting the below, I click the
button before the 10 second sleep finishes, I get the
below output:
console show
proc ptime {arg} {
puts "[clock format [clock seconds]] $arg"
}
pack [button .b -text hello -command helloworld]
proc helloworld {} {
ptime "in helloworld 1"
sleepms 15000
ptime "in helloworld 2"
}
ptime begin
sleepms 10000
ptime after
produces:
Thu Mar 14 19:19:38 PDT 2019 begin
Thu Mar 14 19:19:43 PDT 2019 in helloworld 1
Thu Mar 14 19:19:58 PDT 2019 in helloworld 2
Thu Mar 14 19:19:58 PDT 2019 after
() 1 %