Version 10 of sleep

Updated 2004-04-20 05:02:28

Why does Tcl not come with a sleep command? Why, it just doesn't call it sleep. It calls it after.

However, Jeff Hobbs notes in a discussion held elsewhere:

[someone proposed the code ]

  proc sleep {time} {
      after $time set end 1
      vwait end
  }

[and Jeff responds:]

This [the above code] is actually not correct because vwait must receive a globally scoped variable, so add 'global end' above (or rather use a less common global name).

This has advantages over regular 'after $time' because it does allow other events to continue processing, but that difference should be understood.

Also, when using after that takes a command in Tcl without Tk present, you have to make sure that vwait is called (or update), to kick in the event loop.


This was a horribly misguided thing... dangerously appealing and guaranteed to create snake balls.

I tried to remove the code below from this page, but some evil genius put it back!


Will suspend evaluation of current code block, but allow the event loop to continue handling pending events.

This particular version of uniqkey is my favorite because the keys generated will generally sort in time created order.

 proc uniqkey { } {
     set key   [ expr { pow(2,31) + [ clock clicks ] } ]
     set key   [ string range $key end-8 end-3 ]
     set key   [ clock seconds ]$key
     return $key
 }

 proc sleep { ms } {
     set uniq [ uniqkey ]
     set ::__sleep__tmp__$uniq 0
     after $ms set ::__sleep__tmp__$uniq 1
     vwait ::__sleep__tmp__$uniq
     unset ::__sleep__tmp__$uniq
 }

Example:

 after 4000 puts foo!
 sleep 5000
 puts bar!

-Phil Ehrens


Don't confuse this with such other sleeps as the standard Unix command-line one, or the command Expect builds in, or ...


GPS: You swilly wabbits. :)

 $ tclsh8.4
 % interp alias {} sleep {} after 
 sleep
 % sleep 400
 % interp alias {} wait {} sleep
 wait
 % wait 800