Any way to "send" exit to a handler?

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
soitech
Posts: 11
Joined: Fri Feb 06, 2015 8:09 pm

Any way to "send" exit to a handler?

Post by soitech » Fri Feb 06, 2015 8:25 pm

Is there anything in livecode that is something between "exit" (only halts the handler it is called inside of) and "exit to top" (halts the entire callstack)?

Optimal would be some way to get the currently running handler, then tell it to stop.

I have a feeling this isn't possible, but thought I'd ask in case I've missed something.

Issue is a handler to exit out of a stack and return to a different stack. The handler closes the stack, but as soon as the exit handler is done, execution returns to the handler in a backscript that was running when the exit button was clicked and runs in the background, doing things that need to not happen. Adding "exit to top" to the exit button handler stops the execution, but takes us to the top of the stack we wanted closed, not to the other stack we'd like to return to... and since exit to top halts all execution, I can't just follow the exit to top with a go to...

So best case scenario so far is having the user click the exit button, close the stack, exit to top, stack reappears! click the exit button a second time and now it works.
Not optimal behavior.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Any way to "send" exit to a handler?

Post by dunbarx » Fri Feb 06, 2015 10:31 pm

Hi.

I am not understanding. This happens all the time.

If you have a hierarchy of calls, normally you would want to exit one of the two extreme ends of that hierarchy; one end is the running handler, the other end is the original calling handler. As you said...

But if you manually exit, (perhaps by having the running handler interrogate the state of the mouse or the optionKey, say?) then you can do or go anywhere you want, like send a message to any handler anywhere.

In other words, if manually,
...having the user click the exit button...
you already have that capability. So are you asking for a way to jump over a handler or two in a long string of calls, like (pseudo, or rather fantasy):

Code: Select all

exit runningHandler by going back two calls
Craig Newman

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Any way to "send" exit to a handler?

Post by jacque » Sat Feb 07, 2015 6:59 pm

Separate the closing actions out into another handler in the same script, something like:

Code: Select all

on closeMe
  close this stack 
end closeMe
Then in the handler you want to exit:

Code: Select all

send "closeMe" to me in 0
exit to top
Sending in 0 will execute the close immediately after the scripts are halted.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

soitech
Posts: 11
Joined: Fri Feb 06, 2015 8:09 pm

Re: Any way to "send" exit to a handler?

Post by soitech » Sat Feb 07, 2015 7:06 pm

Thanks for the response.
dunbarx wrote:
But if you manually exit, (perhaps by having the running handler interrogate the state of the mouse or the optionKey, say?) then you can do or go anywhere you want.
Yes, I can go wherever I want, but once I get there, and reach the end of openCard, program execution continues in the backscript that was running when the user hit the exit button, bringing us back to the stack we just closed and away from where we told it to go.

I guess maybe the key isn't to exit the upstream handlers we're done with, but to never let their execution resume, so upon going to the target stack, instead of openCard ending and the application sitting idle until the next user input, have it start a loop that won't stop until the next mouse click.. nah, waste of resources.
dunbarx wrote:So are you asking for a way to jump over a handler or two in a long string of calls
Not looking to jump over anything, more like exit a few handlers in a row, rather than just one or all. Or, actually, exiting one handler at a time, working back up the callstack, but having that message originate outside the handler to exit, since some of the handlers are in mid-execution. Like when the exitButtons handler exits it passes exit along to the next handler, until reaching handler X.

Silly, I know. I did post my question in "Complete Beginners" for a reason. I was obviously barking up the wrong tree.

My guess is the best solution is to redesign some parts of the program to make sure where I want to end up is the top of a stack, then maybe, if I'm lucky, when the user clicks the exit button, I can have it close stack B, go to stack A, then exit to top and end up at the top of stack A waiting for user input. Is that a more sensible approach?

soitech
Posts: 11
Joined: Fri Feb 06, 2015 8:09 pm

Re: Any way to "send" exit to a handler?

Post by soitech » Sat Feb 07, 2015 7:12 pm

jacque wrote:Separate the closing actions out into another handler in the same script, something like:

Code: Select all

on closeMe
  close this stack 
end closeMe
Then in the handler you want to exit:

Code: Select all

send "closeMe" to me in 0
exit to top
Sending in 0 will execute the close immediately after the scripts are halted.
Thanks for the helpful suggestion jacque, that solution might work too. I'll give it a try.

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Re: Any way to "send" exit to a handler?

Post by WaltBrown » Sat Feb 07, 2015 7:19 pm

Maybe make a global called "gExitNow".

Initial it as false.

In every loop in every handler add "if gExitNow then exit" and make sure there is a wait with messages clause in the loop so other event handlers get called.

Create a handler for whatever you want as an exit handler that sets gExitNow to true. For example on keydown. This could also be in an exception handler as well, or some other fatal error detector.

I'm not an expert, there may be other ways, this is what I use.

GExitNow could also be a nesting counter...
Walt Brown
Omnis traductor traditor

soitech
Posts: 11
Joined: Fri Feb 06, 2015 8:09 pm

Re: Any way to "send" exit to a handler?

Post by soitech » Mon Feb 09, 2015 9:00 pm

Thanks for all the tips everyone.

I tried the various suggestions, but due to the nature of the project, and the fact that the stacks I have been trying to exit out of are written by someone else and only alterable to a small extent by me, I ended up altering the program structure to some extent so that the location I am returning to multiple times is a substack rather than just a card. This allowed me to go there and then call "exit to top" and end up where I wanted to be.

Took some work to move everything around, but as with most projects, and especially this being my first real foray into LiveCode, the overall refactoring was beneficial to the whole application.

I'm still having some issues with the stacks I'm closing not actually closing, but it's pretty minor and I'm happy that the exit functionality is 90% working now.

I'll hang on to the other methods that were suggested here, as I'm sure I'll be running into situations where they will be perfect before too long.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Any way to "send" exit to a handler?

Post by dunbarx » Mon Feb 09, 2015 10:00 pm

Hi.

I HATE trying to decipher other peoples code. I can barely decipher my own. What I mean is that if you were successful in doing that, you will have a much easier time with your own projects. Write back often with your progress and problems.

Craig

soitech
Posts: 11
Joined: Fri Feb 06, 2015 8:09 pm

Re: Any way to "send" exit to a handler?

Post by soitech » Tue Feb 10, 2015 5:36 pm

I understand Craig, and trying to decipher someone's vague description of their code and what they are trying to accomplish is even more challenging!

I'm making progress now, I learned a lot getting past this roadblock and now have a better "picture" in my head of how I should be designing my projects to work well.
Thanks for your time!

John

faber3d
Posts: 40
Joined: Sat Nov 16, 2013 6:51 am

Re: Any way to "send" exit to a handler?

Post by faber3d » Fri Mar 04, 2016 3:53 am

Example exit with button for a movile app


on mouseUp

--codig music button app:
--put (specialFolderPath("engine") & "/soundbotones.mp3") into tPath
--play tPath


answer "¿Are you sure you want to quit?" with "NO" or "OK"

if it is "OK" then

quit this stack
close this stack
end if

end mouseUp

Klaus
Posts: 14192
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Any way to "send" exit to a handler?

Post by Klaus » Fri Mar 04, 2016 11:53 am

faber3d wrote:...
quit this stack
...
??? Sure?
Not trying to establish a new user for spamming?

I'll give you one day to explain! 8)

Post Reply