How to abort or exit a loop?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
vamp07
Posts: 21
Joined: Mon Apr 10, 2006 2:12 pm

How to abort or exit a loop?

Post by vamp07 » Mon Oct 19, 2009 4:58 pm

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

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Tue Oct 20, 2009 5:56 am

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

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
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

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
and then add a call to that handler in your loop

Code: Select all

repeat forever
  add 1 to theCounter
  answer theCounter
  CheckForAbort "TightLoop-" & theCounter
end repeat
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

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
and then modify the script with the loop so that it resembles the following

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
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply