Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vwait/sleep? or something...

4 views
Skip to first unread message

Roar Johnsen

unread,
Mar 29, 2001, 2:58:24 AM3/29/01
to
Hi.. I have a simple question..

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 ..

Richard.Suchenwirth

unread,
Mar 29, 2001, 3:17:21 AM3/29/01
to Roar Johnsen
Roar Johnsen wrote:
> 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..
>
proc repeatedly {} {
# your code here
after 120000 repeatedly ;# time in msec
}
repeatedly ;# explicitly starting first time, will restart itself
--
Schoene Gruesse/best regards, Richard Suchenwirth - +49-7531-86 2703
Siemens Production and Logistics Systems AG RC DT2, Konstanz,Germany
Personal opinions expressed only unless explicitly stated otherwise.

Neil Madden

unread,
Mar 29, 2001, 6:41:35 AM3/29/01
to
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 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
--------------------------------------------------------------

Dave Marquardt

unread,
Mar 29, 2001, 11:36:24 AM3/29/01
to
>>>>> "Roar" == Roar Johnsen <roa...@newmedia.no> writes:
Roar> Hi.. I have a simple question.. If have a proc that I want
Roar> to start. every 2 minuts.. how can I do this..

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 LI

unread,
Mar 29, 2001, 6:02:29 PM3/29/01
to
I doubt it can go forever. Finally the stack will overflow.

Chang

Neil Madden wrote in message <3AC31F6F...@cs.nott.ac.uk>...

Bruce Hartweg

unread,
Mar 29, 2001, 6:08:32 PM3/29/01
to

Chang LI wrote:

> I doubt it can go forever. Finally the stack will overflow.
>

why would the stack overflow? It's not recursive.

Bruce

Bruce Hartweg

unread,
Mar 29, 2001, 6:19:59 PM3/29/01
to

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

Richard.Suchenwirth

unread,
Mar 30, 2001, 1:49:00 AM3/30/01
to
Bruce Hartweg wrote:
>
> 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 correct behavior
> proc myProc {args} {
> puts "[llength $args] : $args"
> after 1000 [concat myProc $args]
> }

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.

0 new messages