This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

How to let an utimer know it`s own utimer id?

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
sKy
Op
Posts: 194
Joined: Thu Apr 14, 2005 5:58 pm
Location: Germany

How to let an utimer know it`s own utimer id?

Post by sKy »

Code: Select all

proc lremove { list string } {
 return [lsearch -all -inline -not -exact $listname $string]
}
This sets the timers.

Code: Select all

set timer 0
foreach element $list_of_ tasks {
 set timer [expr $timer + 5]
 set id [utimer $timer [list $element]]
 lappend ::list_with_timerids $id
}
This code is used in case of I want to kill the remaining utimers.

Code: Select all

foreach id $::list_with_timerids {
 killutimer $id
}
The problem is that if the utimer has already been executed then it would throw an error invalid timerid. Sure I could use catch, but I don`t want to use catch.

I don`t want to use catch because some other utimer (set by another script) could have that id now and I could eventually kill the wrong timer because he has the same name.

The problem is set id [utimer $timer [list $element]]. I would like to expand the [list ...] section with a command to remove the utimer from $::list_with_timerids. With set :list_with_timerids [lremove $:list_with_timerids $id].

But this isn`t possible because I get the $id just after I did set the utimer. Is there some tricky work around?

As far I know I can not modify a timer/utimer/after after it has been set. Otherwise it would be easy.
socketapi | Code less, create more.
User avatar
sKy
Op
Posts: 194
Joined: Thu Apr 14, 2005 5:58 pm
Location: Germany

Post by sKy »

No one knows? No way? Or is my English to bad to understand what I want to do?
socketapi | Code less, create more.
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

There is no way to know the id at the time you create the code argument (because it doesn't exist yet).

If all the timers run for the same amount of time, your timer will always be the first on the list, which would let you use lrange to remove its id, but if the delay differs, that won't work...

I have the same problem with my socket api - code executed by the server socket needs to know its id, so I made my own "id" that is used to look up the real id. For timers it would be something like this:

Code: Select all

array set myTimer {}
if {![info exists myTimerID]} {set myTimerID -1}

proc myTimer {delay code} {
	global myTimerID myTimer
	# The custom id is appended to your code and must be used to unset myTimer(theID) if you kill the timer
	set myTimer([incr myTimerID]) [utimer $delay [lappend code $myTimerID]]
	set myTimerID
}

proc killMyTimer id {
	upvar 0 ::myTimer($id) timer
	if {[info exists timer]} {
		killutimer $timer
		unset timer
	}
}
Use the concept in your script instead of using these procs to avoid adding overhead... or use "utimers" in your "kill remaining" code.
Have you ever read "The Manual"?
Post Reply