Version 6 of sleep

Updated 2004-04-15 16:02:24

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


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 ...