We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Overloading the exit() method
Page Index Toggle Pages: 1
Overloading the exit() method (Read 608 times)
Overloading the exit() method
Feb 12th, 2008, 11:25pm
 
Hi all, first time posting here on the forums ...

I'm wondering how to overload the exit() method so that I can close some serial and midi connections as Processing quits.

First I tried this:

Code:
void exit() {
println("test");
}


which works but breaks the exit() method (esc no longer quits)

So I've since been doing this:

Code:
void exit() {
println("test");
System.exit(0);
}


which works and quits out properly ...

Is this the "right way" is there some other more proper way of executing code as processing quits?

Thanks
Sparky
Re: Overloading the exit() method
Reply #1 - Feb 13th, 2008, 12:30am
 
I think the proper way is actually:

Code:
public void stop() { 
//my stop stuff
super.stop()
}
Re: Overloading the exit() method
Reply #2 - Feb 13th, 2008, 12:38am
 
Alright, well that seems to work too ... thanks

Can anyone authoritatively say what's the way to do it? If I call exit() during my app, what code does processing normally run when I'm not overloading exit() (same question for stop()) ?

Sparky
Re: Overloading the exit() method
Reply #3 - Feb 13th, 2008, 12:43am
 
I think you could overload exit() and call super.exit(); to let processing clean up itself, but calling System.exit(); stops processing from closing cleanly.

If you want to see what processing does for stop() and exit() check out the source:
http://dev.processing.org/source/index.cgi/trunk/processing/core/src/processing/core/PApplet.java?view=markup
Re: Overloading the exit() method
Reply #4 - Feb 13th, 2008, 1:09am
 
Ok thanks a lot
Re: Overloading the exit() method
Reply #5 - Feb 13th, 2008, 8:06pm
 
override stop() (and call super.stop()) if you want to add cleanup procedures to your code (shutting down libraries, etc). stop() is not perfect, so it won't always work: http://dev.processing.org/bugs/show_bug.cgi?id=77 but that's the correct mechanism and eventually the bug will be fixed.

exit() is a different mechanism, that will handle calling stop(). it should not be overridden. exit() sets a flag that the app should quit the next time it's safe (as soon as the current setup() or draw() call finishes).

System.exit() kills the application instantly, meaning that no shutdown will happen (stop() won't be called, etc). it should be avoided unless you *really* must quit immediately.
Page Index Toggle Pages: 1