I would like to get help on controlling a procedure in tcl.
For example I create a procedure like below, which basically run
forever.
proc hang {} {
while {1} {
# Do nothing
}
}
Now I want to break above procedure in 300 sec. Means If above
procedure execute more than 300 sec than break the procedure and
execute next command, so that script will not hang. How we can do
that?
-Puneet Goyal.
Use [clock seconds] to get the time. Replace the {1} with a check to see if
the current time is less than 300 sec from the start time.
--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+
you can use after cmd
such as:
set Terminal 1
after 300 set Terminal 0
while {Terminal} {
#do sth
}
may be it can work for you
Puneet and Jimmy, you'll want to read the docs for [after]: the time
value is in milliseconds. And make sure Terminal is a global variable.
set ::Terminal true
after 300000 set ::Terminal false
while {$::Terminal} {
#do sth
}
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
I think this will only work if you do some system call in the
while loop. If not the event loop won't have a chance to run the
set command.
/Str.
To be exact, it will require the [update] (or [vwait]) command to be
run. There are a few things that do that internally ([http::geturl]
for one) but mostly you've got to do it explicitly for the event loop
to be run. With 8.6, you can use coroutines to make it easier by
hiding the event loop away (coronet-style).
Donal.
The only way to interrupt it if it is just spinning like this (ie not
giving any opportunity to process events) is to hit the process with a
signal. Its a while since I did this, but with the TclX extension you
could do this (not on windows):
package require TclX
alarm 300
while {1} {}
This process will "self-destruct" after 300 seconds due to a SIGALRM
being delivered for which it has no handler. The only problem being
that when the SIGALRM gets delivered the whole process will exit. So
you could consider to exec (or even fork using TclX) the code which
needs to get interrupted.
Though generally its probably going to be much simpler (if you have
the means to do this) to make sure your long running proc cooperates
and processes the event loop as others have suggested.
It all depends on what this 300sec proc is meant to be doing.
Regards
Bob
The only way to interrupt it if it is just spinning like this (ie not
giving any opportunity to process events) is to hit the process with a
signal. Its a while since I did this, but with the TclX extension you
could do this (not on windows):
package require TclX
alarm 300
while {1} {}
This process will "self-destruct" after 300 seconds due to a SIGALRM
being delivered for which it has no handler. The only problem being
that when the SIGALRM gets delivered the whole process will exit. So
you could consider to exec (or even fork using TclX) the code which
needs to get interrupted.
Though generally its probably going to be much simpler (if you have
the means to do this) to make sure your long running proc cooperates
and processes the event loop as others have suggested.
It all depends on what this 300sec proc is meant to be doing.
Regards
Bob
>