Re: Pausing without hogging the CPU

Graeme McKerrell (graemem@pdd.3com.com)
Wed, 21 Feb 1996 10:10:18 +0000

The following are the code fragments which I am now using. Since My initial
posting I am now NOT relying on the object's up time, which I believe cannot be
relied on. But am sitting in a loop waiting for an object variable to
change.
My problem is that when 'after' is invoked with just a numeric
argument no other background tasks can get updated until the time
period is over and the next 'update' command is issued. (In my OO
implementation an 'update' is performed before each method call) What
would be useful would be some way of stopping the program flow without
sitting in a tight loop which sends the CPU usage through the roof.

#{{{ method pause {}

#
# wait for a certain number of seconds
#
method pause {time} {
log "pausing for $time seconds"

set start $this(uptime)

set this(paused) true
after [expr $time * 1000] "
$this(this) eval set this(paused) false
"

while {$this(paused)} {
# sleep for 90 milliseconds to give the CPU a rest
# NB any longer would impair uptime counter
# Any shorter sends CPU usage too high!
after 90
this spinner
}
}

#}}}
#{{{ method spinner {}

#
# This displays a spinning symbol every time it is called
# it is based on the spinner variable which is updated at each call
#

method spinner {} {
set spin(0) "\\"
set spin(1) {-}
set spin(2) {/}
set spin(3) {|}
incr this(spinner)
puts -nonewline "\r $spin([expr $this(spinner)%4])\r"
flush stdout
}

#}}}