Abort command with button...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
tjo.mobile
Posts: 18
Joined: Tue Oct 25, 2016 8:09 pm

Abort command with button...

Post by tjo.mobile » Sun Dec 18, 2016 8:22 am

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.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Abort command with button...

Post by Thierry » Sun Dec 18, 2016 8:28 am

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Abort command with button...

Post by SparkOut » Sun Dec 18, 2016 9:23 am

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.

tjo.mobile
Posts: 18
Joined: Tue Oct 25, 2016 8:09 pm

Re: Abort command with button...

Post by tjo.mobile » Sun Dec 18, 2016 10:50 pm

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.

Post Reply