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 › Doing things on exit
Page Index Toggle Pages: 1
Doing things on exit (Read 1576 times)
Doing things on exit
Apr 24th, 2009, 8:29pm
 
Hi, I'm using processing to talk to max/msp, and one of the most annoying things about doing this is remembering to tell max to SHUT UP after I close my processing sketch.

As I have it now, I can press 'Q' (or any other key if i want) to send a signal to max and then call exit(), which is great.

The problem is I never ever ever remember to do this. I always hit command Q for quit, or close the window with the mouse, or hit escape.

Is there a way to have code run on exit automatically, when I close the window or quit out of the applet?
Re: Doing things on exit
Reply #1 - Apr 24th, 2009, 10:41pm
 
I remember another post similar to this but can't remember where...

Edit: stop() is the correct method.

Anyway, there is a stop() method that is supposed to be run when an applet has ended (browser left the page, etc). Unfortunately, there also is no requirement by the JVM to call this method, so there is really no guarantee.

If your program is not running online, you could embed (http://dev.processing.org/reference/core/javadoc/processing/core/PApplet.html) the PApplet into an application window such as a JFrame.

From there, you can use a custom windowListener to take care of shutting things down:

http://java.sun.com/docs/books/tutorial/uiswing/events/windowlistener.html
Re: Doing things on exit
Reply #2 - Apr 25th, 2009, 1:14am
 
Code:
void stop()
{
// Clean and stop stuff
// ...
super.stop();
}

Re: Doing things on exit
Reply #3 - Apr 25th, 2009, 2:08am
 
Oops.

Good catch PhiLho, it is stop(), and not close().
Re: Doing things on exit
Reply #4 - Apr 25th, 2009, 4:26pm
 
Great, thanks.

Interestingly, if i close the window with the mouse, or hit escape, the code inside stop will run. If I hit Command-Q to quit the applet, it doesn't.

Cheesy
Re: Doing things on exit
Reply #5 - Apr 26th, 2009, 1:16am
 
Yes, that's what NoahBuddy said (you were... close! Wink): "Unfortunately, there also is no requirement by the JVM to call this method, so there is really no guarantee."

I guess that when you close the tab, the browser still have control, so quietly and politely advise the applet it is about to be closed.
When you close the browser, you send a system message, telling the application to close as fast as possible (might be the whole system which is shutting down), so perhaps the browser in a hurry saves context but hasn't the time to warn the applets. Pure speculation, and might not apply to all browsers on all systems! Smiley

It is even worse for JavaScript applications, most modern browsers abandoned this "page exiting" event, because it have been badly abused by rogue ads, shoving a popup window when closing the page (and another when closing the popup window, etc.)...

That's unfortunate because it might be so useful for Web applications (releasing a license, closing a database or permanent connection slot, etc.) to handle this event.
Re: Doing things on exit
Reply #6 - Apr 18th, 2010, 6:11pm
 
Great info.  I'm doing some motor control stuff so stopping the motor on app exit is really important.  I'm having the same problem where exiting via cmd+Q does not execute stop().  Is there a way to handle this?

Thanks,
Mike
Page Index Toggle Pages: 1