Page 1 of 1

Abort command with button...

Posted: Sun Dec 18, 2016 8:22 am
by tjo.mobile
I have a command that can take a long time to complete, so I wanted to give the user a way to abort the running command in case they do not want to wait for the command to complete. The command uses a repeat loop that may run several thousand times.

I created an 'ABORT' button and added the following code:

Code: Select all

on mouseup

put 1 into gAbort

end mouseup
Within the repeat loop I added this code:

Code: Select all

if gAbort = 1 then

exit myslowcommand

end if
However once the command is running the button does not seem to do anything. It seems as though the running command cannot see that the variable has changed, or that the button is not able to process the mouseup request until the command has finished running? I listed gAbort as a global in both the button and card script.

Is there a way to have the updated variable passed to the command while it is running?

Thanks for any advice or suggestions,

TJ.

Re: Abort command with button...

Posted: Sun Dec 18, 2016 8:28 am
by Thierry
Hi TJ,

Code: Select all

global gAbort

on yourHandler
    .....
   if gAbort = 1 then
   put 0 into gAbort
   exit myslowcommand
    .....
end if

on mouseUp
   put 1 into gAbort
end mouseUp
if yourHandler and the mouseUp are not in the same script (card, stack,..)
you need to type the global gAbort on top of each script.

HTH,

Thierry

Re: Abort command with button...

Posted: Sun Dec 18, 2016 9:23 am
by SparkOut
Inside the repeat loop you need to add an extra line:

Code: Select all

wait 0 milliseconds with messages  
This lets the engine grab a moment of as near zero duration as possible to allow it to react to keystrokes, button clicks and other housekeeping and stops your application from becoming unresponsive.

Re: Abort command with button...

Posted: Sun Dec 18, 2016 10:50 pm
by tjo.mobile
Thank you both for your advice.

I did have the variables listed as globals in the button and card script.

The "wait 0 milliseconds with messages" worked perfectly. I did have some "wait 1 tick" lines in the repeat loop, but I'm guessing it was the "with messages" part that did the trick.

Thanks again for the help, very much appreciated.

TJ.