Page 1 of 2
stopping a repeated script with a button
Posted: Tue Feb 08, 2011 7:33 am
by magice
I need a script to run every 15 seconds until it is turned off by the user. My original thought was to set a control variable to true with a button, and use "repeat while controlvariable is true". Then have a button that sets the control variable to false to kill the loop. unfortunately, while the loop is running none of the buttons function. There is a 15 second wait in the loop that I thought would allow for other input, but I guess it doesn't work that way. Is there a way to give the loop a break long enough to allow a button to be pushed?
Re: stopping a repeated script with a button
Posted: Tue Feb 08, 2011 8:44 am
by jmburnod
Hi Magice,
Try this
Code: Select all
on mouseUp
repeat until the mouse is down
put the seconds
wait 1 milliseconds with messages
end repeat
end mouseUp
Best
Jean-Marc
Re: stopping a repeated script with a button
Posted: Tue Feb 08, 2011 12:17 pm
by BvG
if the 15 minutes do not need to be kept exactly, you can also use send in time. That is guaranteed to be nonblocking, but it might not fire, until the mouse is released (for example when the user is resizing the stack), or repeat loops have finished.
on mouseUp
send "runTimer" to me in 15 seconds
end mouseUp
on runTimer
--ca. 15 seconds later
end runTimer
Re: stopping a repeated script with a button
Posted: Tue Feb 08, 2011 2:42 pm
by magice
Thank you Jean-Marc, I tried your suggestion to understand exactly what it does. Unfortunately running the loop until mouse is down only stops the loop while the mouse is down. as soon as I release to mouse again it reactivates the mouseup command so I am not quite sure how to work this into what I have done. Here is what I have so far"
Code: Select all
-- this is the code for the start button
on mouseUp
send activateToggle to me in 1 milliseconds
end mouseUp
on activateToggle
global tLogStarted
set the visible of me to false
set the visible of button LogToggle2 to "true"
put "true" into tLogStarted
beginPollingLog
end activateToggle
--This is the code for the stop button
on mouseUp
send deactivateToggle to me in 1 milliseconds
end mouseUp
on deactivateToggle
global tLogStarted
set the visible of me to false
set the visible of button LogToggle to "true"
put "false" into tLogStarted
end deactivateToggle
I started with a single button that toggled label and script, but changed to a two button system with buttons stacked in the same position in hopes that it would solve the problem.
the script the first button runs is the repeat loop which is basically like this:
Code: Select all
on beginPollingLog
global tLogStarted
repeat while tLogStarted is "true"
--script i want to run every 15 seconds until stopped by the user
wait for 15 seconds
end repeat
end beginPollingLog
After pushing the start button no buttons will work. I have to shut down rev completely and reload the program. When I shut down rev the repeat loop aborts at the wait command, which seems to me eliminates the actual script as the problem.
Re: stopping a repeated script with a button
Posted: Tue Feb 08, 2011 3:28 pm
by dunbarx
Make a button, and make sure its "autoHilite" is set to "false". Put these two handlers into its script
Code: Select all
on mouseUp
set the hilite of me to not the hilite of me
if the hilite of me then startTimer
end mouseUp
on startTimer
if not the hilite of me then exit startTimer
repeat 30
if the mouseClick then
exit to top
set the hilite of me to "false"
end if
put random(99)
wait 5
if the mouseClick then
set the hilite of me to "false"
exit to top
end if
end repeat
if the hilite of me then send startTimer to me in 5 seconds
end startTimer
Is this the sort of thing you needed?
Craig Newman
Re: stopping a repeated script with a button
Posted: Tue Feb 08, 2011 4:13 pm
by magice
Mr.Newman Thank you very much. This is not exactly the button behavior I wanted, but with a little modification it works to stop my repeat loop. And I am actually starting to like this button behavior more then the name change that i was wanting before. So basically you fixed my problem.
for anyone with a similar situation, this is the modified version of your script that runs my script every 15 seconds until stopped.
Code: Select all
on mouseUp
set the hilite of me to not the hilite of me
if the hilite of me then startTimer
end mouseUp
on startTimer
if not the hilite of me then exit startTimer
repeat while the hilite of me
if the mouseClick then
exit to top
set the hilite of me to "false"
end if
beginPollingLog
wait 15 seconds
if the mouseClick then
set the hilite of me to "false"
exit to top
end if
end repeat
if the hilite of me then send startTimer to me in 5 seconds
end startTimer
I then just removed the repeat and the wait line from my "beginPollingLog" scrips and is seems to work. of course there is up to a 15 second delay when toggling the button off before seeing the hilite change, but I can live with that if it means my project is no longer dead. Thank you all again for your suggestions and thank you Mr. Newman for your solution.
Re: stopping a repeated script with a button
Posted: Tue Feb 08, 2011 6:16 pm
by dunbarx
You're welcome.
But you are using a "wait" command, which will lock the system until it times out. Is this OK? I would continue to modify so that no "wait" is ever needed. The one I used was only to make the short sequence of random numbers visible to represent another process.
Also, though you like the hilite, why not have both? When the hilite is true, set the label of the button to "running" (or something). When it is false, set the label to "push me".
Re: stopping a repeated script with a button
Posted: Wed Feb 09, 2011 3:13 pm
by jsburnett
I have no solution to offer but just wonder in general, how are "clocks" scripted that preform a continous script but don't "lock" the system?
Re: stopping a repeated script with a button
Posted: Wed Feb 09, 2011 4:34 pm
by dunbarx
Generally the "send message in time" command is used. Instead of "wait" or "repeat until...", this command leaves the system alone. Check the dictionary.
Craig Newman
Re: stopping a repeated script with a button
Posted: Wed Feb 09, 2011 7:12 pm
by dunbarx
Here is a simple script that shows "send in time". Put these handlers into a button. You can do other tasks while the message box happily counts...
Code: Select all
on mouseUp
set the timeKeeper of me to 0
startTimer
end mouseUp
on startTimer
set the timeKeeper of me to the timeKeeper of me + 1
put the timeKeeper of me
if the timeKeeper of me < 25 then
send "startTimer" to me in 20
end if
end startTimer
Check out the "pendingMessages" (and related stuff) in the dictionary. They are key to managing this sort of thing.
Craig Newman
Re: stopping a repeated script with a button
Posted: Wed Feb 09, 2011 8:16 pm
by magice
This thread has been most helpful in understanding what I am doing to the system when I create a loop. One thing I have noticed, is that if I close the stack without first toggling off the repeat loop, the stack closes but the process still runs in windows background. Is there an easy way to kill the loop before allowing the stack to close?
Re: stopping a repeated script with a button
Posted: Wed Feb 09, 2011 11:07 pm
by jmburnod
Hi Magice
Is there an easy way to kill the loop before allowing the stack to close?
Yes. on CloseStack (or when you want)
Code: Select all
on closestack
repeat for each line aLine in the pendingMessages
if aLine contains "startTimer" then
cancel item 1 of aLine
end if
end repeat
end closestack
Best
Jean-Marc
Re: stopping a repeated script with a button
Posted: Thu Feb 10, 2011 11:49 am
by Klaus
Bonjour Jean-Marc,
yep that's the way to canel "pending messages", but obviously magice still has "wait XYZ" and "repeat until..."
clauses in his handlers which will of course NOT be interrupted by this script!
@magice
AVOID "wait xyz" and "repeat until..." whereever you can!
That will save you from a lot of headaches!
Best
Klaus
Re: stopping a repeated script with a button
Posted: Fri Feb 11, 2011 5:41 pm
by magice
I have seen the error of my ways. After finally having the logic of how the startTimer system works, I did a complete rewrite of my script. It now looks like this:
Code: Select all
on mouseUp
global tButtonToggle
if tButtonToggle is false then
set the label of me to "Stop"
put true into tButtonToggle
send startTimer to me in 1 millisecond
else
set the label of me to "Start"
put false into tButtonToggle
end if
end mouseUp
on startTimer
global tButtonToggle
if tButtonToggle is false then
exit startTimer to top
else
beginPollingLog
send startTimer to me in 5 seconds
end if
end startTimer
My processor usage went down from 20% to less than 1% and I no longer leave stray processes running on shutdown. Thank you.
Re: stopping a repeated script with a button
Posted: Fri Feb 11, 2011 6:10 pm
by Klaus
Hi Magice,
wonderful, congratulations!
Please allow me some more tiny hints
See my comments
Code: Select all
on mouseUp
global tButtonToggle
if tButtonToggle is false then
set the label of me to "Stop"
put true into tButtonToggle
## send startTimer to me in 1 millisecond
## Yopu can simply CALL the handler now, no need to send
startTimer
else
set the label of me to "Start"
put false into tButtonToggle
end if
end mouseUp
on startTimer
global tButtonToggle
if tButtonToggle is false then
## exit startTimer to top
exit startTimer
## OR
## exit to top
else
beginPollingLog
## Put quotes around the name of the messages to send!
send "startTimer" to me in 5 seconds
end if
end startTimer
Best
Klaus