View Single Post
Old 11-21-2003, 01:25 PM  
darkone
Disabled
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default

Hmms, it shouldn't execute more than once per command You need to recall timer once per script run... so something like this should work:


mytimerscript.itcl

# do stuff
...
# stuff done

timer "TCL mytimerscript.itcl"


also you could protect your timer, so that there won't be multiple instances... here's a working hack, using spinlock - it might crash, if timer is stopped twice


proc mytimer_do { wo } {
putlog "timer is running"
timer 1000 "TCL ../scripts/timertest.itcl -timer $wo"
}


proc mytimer { wo } {
# Set status to unsignaled & return current status (0 = signaled, 1 = unsignaled)
if {[waitobject reset $wo]} {
# Timer has not been canceled
mytimer_do $wo
return
}
putlog "closing timer"
# Close waitobject permanently
waitobject delete $wo
return
}


if {[info exists args]} {
set argv [split $args]
# "-timer" is called by timer
if {[lindex $argv 0] == "-timer"} {
mytimer [lindex $argv 1]
return 0
}

# "-stop" is a parameter that stops the timer, on next timer instance
if {[lindex $argv 0] == "-stop"} {
set wo [waitobject open "myprotector" spinlock]
if {$wo} {
# Set status to signaled
putlog "Stopping timer"
if {[waitobject set $wo]} {
# Could not acquire lock, this means timer is running
iputs "Stopping timer"
} else {
# Acquired lock, this means that waitobject didn't exist
#
waitobject delete $wo
}
waitobject close $wo
}
return 0
}
}


# Open named spinlock
set wo [waitobject open "myprotector" "spinlock"]
if {$wo} {
# Spinlock open, wait - max 1spin (if lock can't be instantly acquired, timer already exists)
if {[waitobject wait $wo 0] == 0} {
# Acquired lock
iputs "Setting up new timer!"
mytimer_do $wo
} else {
# Timer is already running
iputs "Timer already running!"
}
waitobject close $wo
return 0
} else {
# Couldn't create spinlock
return 1
}
darkone is offline