Hi,
I have a loop where I placed the answer command to monitor a variable. The loop runs for several hundred iterations. How do I get out of it if I am not in debug mode and command-. does not work?
Thanks
Steven
How to abort or exit a loop?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Hi Steven,
It's a place we've all been at least once, and I'm afraid there's no positive response: you can't get out in a normal fashion. That's why my tight loops usually have an 'escape route' like this
The cantAbort property of a stack determines whether or not you can abort in the first place (something which you really ought to turn on in standalones) and there's a checkbox on the stack inspector so it's quite convenient.
Another option is to move that block of code into a separate handler at the stack level
and then add a call to that handler in your loop
The downside of the second approach is that any cleanup code that comes after the repeat won't be called. Of course you can also rely on try-throw-catch exception handling, but some people find this awkward.
So just for completion, here's how you'd allow escape with cleanup via exception handling; first change the CheckForAbort handler to
and then modify the script with the loop so that it resembles the following
HTH,
Jan Schenkel.
It's a place we've all been at least once, and I'm afraid there's no positive response: you can't get out in a normal fashion. That's why my tight loops usually have an 'escape route' like this
Code: Select all
repeat forever
add 1 to theCounter
answer theCounter
-- next block allows escape
if the cantAbort of this stack is false and the shiftKey is down then
answer "Are you sure you want to exit the loop after running" && theCounter && "times?" with "Continue" or "Exit"
if it is "Exit" then exit repeat
end if
end repeat
Another option is to move that block of code into a separate handler at the stack level
Code: Select all
on CheckForAbort pCounter
if the cantAbort of this stack is false and the shiftKey is down then
answer "Are you sure you want to abort execution of" && pInfo && "?" with "Continue" or "Exit"
if it is "Exit" then exit to top
end if
end CheckForAbort
Code: Select all
repeat forever
add 1 to theCounter
answer theCounter
CheckForAbort "TightLoop-" & theCounter
end repeat
So just for completion, here's how you'd allow escape with cleanup via exception handling; first change the CheckForAbort handler to
Code: Select all
on CheckForAbort pInfo
if the cantAbort of this stack is false and the shiftKey is down then
answer "Are you sure you want to abort execution of" && pInfo && "?" with "Continue" or "Exit"
if it is "Exit" then throw "Aborting execution of" && pInfo
end if
end CheckForAbort
Code: Select all
try
repeat forever
add 1 to theCounter
answer theCounter
CheckForAbort "TightLoop-" & theCounter
end repeat
catch tError
answer error tError
finally
-- do your cleanup here
--> it will be called whether or not something throws an error
end try
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com