Page 1 of 1

Any way to "send" exit to a handler?

Posted: Fri Feb 06, 2015 8:25 pm
by soitech
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.

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

Posted: Fri Feb 06, 2015 10:31 pm
by dunbarx
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

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

Posted: Sat Feb 07, 2015 6:59 pm
by jacque
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.

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

Posted: Sat Feb 07, 2015 7:06 pm
by soitech
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?

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

Posted: Sat Feb 07, 2015 7:12 pm
by soitech
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.

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

Posted: Sat Feb 07, 2015 7:19 pm
by WaltBrown
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...

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

Posted: Mon Feb 09, 2015 9:00 pm
by soitech
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.

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

Posted: Mon Feb 09, 2015 10:00 pm
by dunbarx
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

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

Posted: Tue Feb 10, 2015 5:36 pm
by soitech
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

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

Posted: Fri Mar 04, 2016 3:53 am
by faber3d
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

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

Posted: Fri Mar 04, 2016 11:53 am
by Klaus
faber3d wrote:...
quit this stack
...
??? Sure?
Not trying to establish a new user for spamming?

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