Page 1 of 1

why is the Stop button not working in 2nd stack?

Posted: Fri Nov 22, 2013 3:44 am
by keram
Hi,

I made 2 stacks to test the Stop button for repeat loop. In one case it's working OK, but in the second it does not. Why is it so?

Here is the code and attached stacks.

working OK:
stack script:

Code: Select all

global startNextT

on startNext
   
   put empty into field "result"  
 
   repeat fld "reps" times         
      put "X" after fld "result"         
      wait 20 ticks      
   end repeat   
   
   send "startNext" to me in 120 ticks
   put the result into startNextT
   
end startNext
Start button:

Code: Select all

on mouseUp
   startNext
end mouseUp
Stop button:

Code: Select all

global startNextT
on mouseup
  cancel startNextT
end mouseup
---------------
Stop button not working:
stack script:

Code: Select all

global startNextT

on startNext
      repeat with countVar = 1 to 20        
      put countVar +1 into fld "counter"          
      wait 20 ticks      
   end repeat   
   
   send "startNext" to me in 120 ticks
   put the result into startNextT
   
end startNext
Start button:

Code: Select all

on mouseUp
   startNext
end mouseUp
Stop button:

Code: Select all

global startNextT
on mouseup
  cancel startNextT
end mouseup
Thanks for clarifying this.

keram

Re: why is the Stop button not working in 2nd stack?

Posted: Fri Nov 22, 2013 6:55 am
by Robertel
Hi Keram,

I copied and pasted your scripts into the simplest stacks consistent with those scripts and the stop button worked in both for me. The second stack worked with no changes at all from what you posted. In the first stack I replaced

Code: Select all

repeat fld "reps" times 
with

Code: Select all

repeat for 5 times 
Robertel

Re: why is the Stop button not working in 2nd stack?

Posted: Fri Nov 22, 2013 9:37 am
by keram
Thanks Robertel,

Yes, It is working as you said - it was my misunderstanding. I assumed that in the second one I could stop the current run (from 1-20) but the Stop button is stopping the next repeat of the '1-20 run'.

keram

Re: why is the Stop button not working in 2nd stack?

Posted: Fri Nov 22, 2013 11:43 am
by AndyP
Hi Keram,

The problem is there is no time in the loop for your Stop button mouseUp message to get caught and actioned.

Try this:

Stack script
-------------------

global startNextT
global gButtonClicked // new global to get Stop button status
on startNext
repeat with countVar = 1 to 20
put countVar +1 into fld "counter"
wait 20 ticks with messages // added 'with messages' which allows time for message to get through
if gButtonClicked is "true" then // exit routine
exit to top // Halts the current handler and all pending handlers.
end if
end repeat
send "startNext" to me in 120 ticks
put the result into startNextT
end startNext


Start button
-----------------

global gButtonClicked
on mouseUp
put "false" into gButtonClicked
startNext
end mouseUP


Stop button
-----------------

global gButtonClicked
on mouseUp
put "true" into gButtonClicked
end mouseUp

Re: why is the Stop button not working in 2nd stack?

Posted: Sat Nov 23, 2013 2:25 am
by keram
Hi Andy,

Thanks for your corrections - it worked OK.
I modified it slightly - want to resume from where it stopped and not from the beginning.
How to do that?

Script is here:

Stack script:
global startNextID,gButtonClicked,resume
on startNext
repeat with countVar = 0 to 10
put countVar +1 into fld "counter"
wait 20 ticks with messages // added 'with messages' which allows time for message to get through
if gButtonClicked is "true" then // exit routine
put fld "counter" into resume
put resume into fld "resume"

## something has to go in here but I don't know what :(

exit to top // Halts the current handler and all pending handlers.
end if
wait for 60 ticks
end repeat
end startNext

-----

Start button:
global startNextID,gButtonClicked,resume
on mouseUp
put "false" into gButtonClicked
startNext
end mouseUp

--------------

Stop button:
global gButtonClicked,resume
on mouseUp
put "true" into gButtonClicked
end mouseUp

-----------------

Also, I'm wondering, when I add a new variable like resume, do I have to add it as global and everywhere - not only on the stack but on all buttons as well?

keram