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.
Page Index Toggle Pages: 1
stop() (Read 5062 times)
stop()
Jun 9th, 2009, 12:49pm
 
I was looking at some sample code that uses the Ess library from Krister Olsson. The code always ends with a stop() function. For instance:
Code:
// clean up Ess before exiting

public void stop() {
 Ess.stop();
 super.stop();
}


I'm assuming this is a built-in function, like setup() and draw(), but I can't find any information about it. I looked through the Reference here on the web site and through Ben & Casey's book, to no avail. What is it? When do we need it? Is it for doing something like releasing system resources when the program ends?

Thanks!
Re: stop()
Reply #1 - Jun 9th, 2009, 12:59pm
 
Yes!  stop() is called whenever a sketch is closed/quit/exited.

If you have any clean up items you need taken care of before your sketch exits, you can create your own stop() function, and then just reference super.stop() at the end (as shown in your example code).  The super.stop() bit just calls the Processing/Java built-in stop() method.

Oh, so in this case, the Ess author just wanted to ensure that some Ess-related clean-up would occur on quit.
Re: stop()
Reply #2 - Jun 9th, 2009, 2:51pm
 
Great - thanks, Scott!

(Shouldn't there be something about this in the Reference?)
Re: stop()
Reply #3 - Jan 4th, 2010, 1:21am
 
It is interersting :

i try to alert users if no save before closing sketch,(for exemple after hit the cross window)
but my text is no draw before the window desapear!

i tried without super.stop() : same result so i don't know the rule of super.stop()...

how can i prevent the window closing and the sketch exit ?
Re: stop()
Reply #4 - Jan 4th, 2010, 2:18am
 
Scott Murray wrote on Jun 9th, 2009, 12:59pm:
Yes!  stop() is called whenever a sketch is closed/quit/exited.

If you have any clean up items you need taken care of before your sketch exits, you can create your own stop() function, and then just reference super.stop() at the end (as shown in your example code).  The super.stop() bit just calls the Processing/Java built-in stop() method.

Oh, so in this case, the Ess author just wanted to ensure that some Ess-related clean-up would occur on quit.


That would make sense:  I've noticed there are sketches on OpenProcessing where the audio continues after the webpage has been left; in fact the only way to stop it is to close the browser.  Presumably these people haven't implemented the required 'cleanup' in stop()...
Re: stop()
Reply #5 - Jan 4th, 2010, 6:08am
 
Quote:
how can i prevent the window closing and the sketch exit ?

It is hard to do because Processing sketches are applets, even if running on the desktop, and applets are probably not allowed to stop a browser or tab to be closed.

Maybe you can do some operations on exit, like showing dialog boxes, asking for a file name to save something, etc. but I believe you can't display something on the sketch's surface nor prevent closing.

Quote:
i don't know the rule of super.stop()

The role of super.stop() is to call PApplet's stop() method, which clean up allocated resources, which can be important with some libraries (precisely to stop sound, to free com ports, etc.).

...

OK, I found a way to do whatever we want on closing the sketch.
By default, AWT frames do nothing when the system tries to close them (close button, close action on task bar's context menu, etc.). Processing has to tell to do a System.exit(0) call upon this event to close the window. It does that by adding a window listener called on window closing.

So I had the idea to remove this window listener and putting my own...
Looks like:
Code:
void ChangeWindowListener()
{
 WindowListener[] wls = frame.getWindowListeners();
 println("Found " + wls.length + " listeners");
 if (wls.length > 0)
 {
   frame.removeWindowListener(wls[0]); // Suppose there is only one...
   frame.addWindowListener(new WindowAdapter()
   {
public void windowClosing(WindowEvent we)
{
   println("Should be closing!");
   // Do something useful here
}
   });
 }
}

void draw()
{
 if (frameCount == 1) // Important, before, listener isn't set
 {
   ChangeWindowListener();
 }
 // Your draw code
}

So you can ignore close requests altogether, although you should provide an alternative way to close the window to avoid angry users... Smiley

[EDIT] A simple example of confirmation:
[EDIT 2] Now also handling the Escape exit route...
Code:

void keyPressed()
{
 // Do keyPressed stuff, if needed
 
 // Also prevent direct exit from Escape key, ask confirmation first
 if (key == KeyEvent.VK_ESCAPE)
 {
   key = 0;
   ConfirmExit();
 }
}

// Testing preventing accidental window closing...
void ChangeWindowListener()
{
 WindowListener[] wls = frame.getWindowListeners();
 //println("Found " + wls.length + " listeners");
 if (wls.length > 0)
 {
   frame.removeWindowListener(wls[0]); // Suppose there is only one...
   frame.addWindowListener(new WindowAdapter()
   {
public void windowClosing(WindowEvent we)
{
   //println("Should be closing!");
   ConfirmExit();
}
   });
 }
}

void ConfirmExit()
{
 int exitChoice = javax.swing.JOptionPane.showConfirmDialog(frame,
"You have unsaved changes. Are you sure you want to exit?",
"SketchName - Confirm exit",
javax.swing.JOptionPane.YES_NO_OPTION
 );
 if (exitChoice == javax.swing.JOptionPane.YES_OPTION)
 {
   exit();
 }
}
Page Index Toggle Pages: 1