Timeouts functions

9 views
Skip to first unread message

Serge Fenet

unread,
Aug 19, 1999, 3:00:00 AM8/19/99
to
Hello

Gtk has a nice timeout function. For example, 'gtk_timeout_add(10000,
hop, NULL);' let us call the proc 'hop' every ten seconds.
What is the simplest way to do it in Tcl/Tk ?
I made it with an 'open "|toto"' with an 'after' in toto, but it seems
rather ugly to me.. :)
Thanks in advance

------
Sergei

laurent....@cgi.ca

unread,
Aug 19, 1999, 3:00:00 AM8/19/99
to

Something like:

proc hop {} {
puts "In hop!"
after 10000 {hop}
}

hop

?

L

--
Penguin Power! Nothing I say reflects the views of my employer

Laurent Duperval mailto:laurent....@cgi.ca
CGI - FWFM Project Phone: (514) 391-9523

Jeffrey Hobbs

unread,
Aug 19, 1999, 3:00:00 AM8/19/99
to Serge Fenet
Serge Fenet wrote:
> Gtk has a nice timeout function. For example, 'gtk_timeout_add(10000,
> hop, NULL);' let us call the proc 'hop' every ten seconds.
> What is the simplest way to do it in Tcl/Tk ?

In general, the after command is the Tcl level timeout handler.
If you want a rescheduler proc, try:

# every --
# Cheap rescheduler
# every <time> cmd; # cmd is a one arg (cmd as list)
# schedules $cmd to be run every <time> 1000ths of a sec
# IOW, [every 1000 "puts hello"] prints hello every sec
# every cancel cmd
# cancels a cmd if it was specified
# every info ?pattern?
# returns info about commands in pairs of "time cmd time cmd ..."
#
proc every {time {cmd {}}} {
global EVERY
if {[regexp {^[0-9]+$} $time]} {
# A time was given, so schedule a command to run every $time msecs
if {[string compare {} $cmd]} {
set EVERY(TIME,$cmd) $time
set EVERY(CMD,$cmd) [after $time [list every eval $cmd]]
} else {
return -code error "wrong \# args: should be \"[lindex [info level 0]
0] <number> command"
}
return
}
switch $time {
eval {
if {[info exists EVERY(TIME,$cmd)]} {
uplevel \#0 $cmd
set EVERY(CMD,$cmd) [after $EVERY(TIME,$cmd) \
[list every eval $cmd]]
}
}
cancel {
if {[string match "all" $cmd]} {
foreach i [array names EVERY CMD,*] {
after cancel $EVERY($i)
unset EVERY($i) EVERY(TIME,[string range $i 4 end])
}
} elseif {[info exists EVERY(CMD,$cmd)]} {
after cancel $EVERY(CMD,$cmd)
unset EVERY(CMD,$cmd) EVERY(TIME,$cmd)
}
}
info {
set result {}
foreach i [array names EVERY TIME,$cmd*] {
set cmd [string range $i 5 end]
lappend result $EVERY($i) $cmd
}
return $result
}
default {
return -code error "bad option \"$time\": must be cancel, info or a
number"
}
}
return
}


--
Jeffrey Hobbs The Tcl Guy
jeffrey.hobbs at scriptics.com Scriptics Corp.

Reply all
Reply to author
Forward
0 new messages