Execute code when sketch stops

I’m trying to execute code when a sketch stops. I’ve been using this as a guide (Run code on exit - Processing Forum). There seems to be three scenarios that this can occur 1) Closing the window 2) Hitting the ESC key 3) Pressing the stop button in Processing. I have the first two working but not the stop button. My guess is that this is not calling stop()/exit() but instead some sort of killall process - but searching I can’t find what this is. How do I capture this or is it impossible because it’s from another window?

Here is what I have. I’m testing in Processing 4.3 on OSX.

void setup()
{
  size(200, 200);

  prepareExitHandler();
}


void draw()
{

}


/*
void dispose()
{
  println("Disposing");
  super.stop();
  super.exit();
}
*/


void stop()
{
  println("Stopping");
  super.stop();
  super.exit();
}


void exit()
{
  println("Exiting");
  super.stop();
  super.exit();
}


private void prepareExitHandler()
{
  Runtime.getRuntime().addShutdownHook(new Thread(new Runnable()
  {
    public void run()
    {

      System.out.println("Shutdown Hook");
    }
  }));
}

Hi @garrettlynch,

just like this …

Cheers
— mnse

void setup() {
   size(500,500);  
}

void draw() {
   background(0);
}

// do stuff you wanna do inside exit function ...
void exit() {
   println("Exiting!");
}

Hi @mnse

There is an exit() function is the code i posted and as I said this does not work on Processing 4.3 on OSX. Stopping the sketch with the stop button does not trigger it.

Hi @garrettlynch,

may I ask you, why you use the Stop-Button ? This button is meant to be to force kill your application. Usually one uses the close button on the sketch window, or uses a key handler for ESC key to close the window…

So briefly spoken, you can’t catch the Stop-Button event as it kills the underlying VM which is your sketch running…

For regular closing events you can use the method shown above …

Cheers
— mnse

1 Like

Hi

why you use the Stop-Button ?

I’m trying to catch all possible scenarios of the sketch stopping as my original post mentioned.

You mentioned the typical ways of stopping a sketch. The ESC key doesn’t seem to work properly on OSX with Processing 4.3 anymore. It used to but now it stops the sketch but leaves the window open.

This button is meant to be to force kill your application…you can’t catch the Stop-Button event

This answers my question - it is not possible to catch that kill all event.

Many thanks.