If have a proc that I want to start. every 2 minuts.. how can I do
this..
When i start the script. It runs the proc. when it finnishes wait 2
minuts ( or another interval ) and then runs the proc again..
And does this forever..
Best Regards.. :)
Roar ..
proc myProc {args} {
# Do stuff...
after 120000 [list myProc $args]
# Time is in milliseconds, hence 120000=2 minutes.
}
You must make sure you a running an event loop for this to work. This
means, either running in wish, or adding a "vwait forever" at the end of
the script.
--
--------------------------------------------------------------
Neil Madden. |
Personal: | Computer Science:
ne...@tallniel.co.uk | nem...@cs.nott.ac.uk
http://www.tallniel.co.uk | http://www.cs.nott.ac.uk/~nem00u
--------------------------------------------------------------
Roar> When i start the script. It runs the proc. when it finnishes
Roar> wait 2 minuts ( or another interval ) and then runs the proc
Roar> again.. And does this forever..
Check out the "after" command.
--
Dave Marquardt
Sun Microsystems, Inc.
+1 512 401-1077
Chang
Neil Madden wrote in message <3AC31F6F...@cs.nott.ac.uk>...
Chang LI wrote:
> I doubt it can go forever. Finally the stack will overflow.
>
why would the stack overflow? It's not recursive.
Bruce
Neil Madden wrote:
> At the end of the proc, use [after] to schedule running the proc again:
>
> proc myProc {args} {
> # Do stuff...
> after 120000 [list myProc $args]
> # Time is in milliseconds, hence 120000=2 minutes.
> }
>
You should use concat, not list to build the after command.
By using list you are adding an extra level of nesting to the arguments.
#Example of problem
proc myProc {args} {
puts "[llength $args] : $args"
after 1000 [list myProc $args]
}
myProc a b c d
Results:
4 : a b c d
1 : {a b c d}
1 : {{a b c d}}
1 : {{{a b c d}}}
1 : {{{{a b c d}}}}
1 : {{{{{a b c d}}}}}
1 : {{{{{{a b c d}}}}}}
1 : {{{{{{{a b c d}}}}}}}
1 : {{{{{{{{a b c d}}}}}}}}
1 : {{{{{{{{{a b c d}}}}}}}}}
1 : {{{{{{{{{{a b c d}}}}}}}}}}
1 : {{{{{{{{{{{a b c d}}}}}}}}}}}
1 : {{{{{{{{{{{{a b c d}}}}}}}}}}}}
1 : {{{{{{{{{{{{{a b c d}}}}}}}}}}}}}
#Example of correct behavior
proc myProc {args} {
puts "[llength $args] : $args"
after 1000 [concat myProc $args]
}
myProc a b c d
Results:
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
4 : a b c d
Bruce
This indeed has the correct behavior, but you're still working too
hard... From 'after' man page: "after ms ?script script script ... The
delayed command is formed by concatenating all the script arguments in
the same fashion as the concat command. " 'uplevel' has a similar
behavior.
So in this case it is sufficient to write:
after 1000 myProc $args
But in general, because concat removes one level of list wrapping, using
[list] on the after script is not a bad idea (otherwise a string like
"foo bar" would be broken up into two words). It's just the special
feature that 'args' as a final formal parameter to a proc collects the
rest of the arguments into one list. For all the flexibility of the
'args' construct,
proc try {args} {
puts [list [llength $args] args: $args]
}
reacts of course differently when called with
try foo bar
resp.
try {foo bar}
If you want to unify these two cases, just drop the line
if {[llength $args]==1} {set args [lindex $args 0]}
early in the proc with the 'args' argument. This kind of flexibility is
gradually being introduced into Tk, where item coordinates may either
come flat or in a list, reducing the need for [eval]. The same might be
considered for geometry managers: I still often write
eval pack [winfo children .]
where the eval would not be needed if pack accepted a list of widget
names as well.