Version 2 of sleep

Updated 2002-09-11 12:06:21

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


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