# Execute code when sketch stops

**URL:** <https://discourse.processing.org/t/execute-code-when-sketch-stops/42868>\
**Category:** Coding Questions\
**Created:** [October 1, 2023, 2:47pm UTC](https://discourse.processing.org/t/execute-code-when-sketch-stops/42868 "2023-10-01T14:47:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![garrettlynch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/garrettlynch/32/19853_2.png) [@garrettlynch](https://discourse.processing.org/u/garrettlynch)\
**Post date:** [October 1, 2023, 2:47pm UTC](https://discourse.processing.org/t/execute-code-when-sketch-stops/42868/1 "2023-10-01T14:47:13Z")

</div>

I’m trying to execute code when a sketch stops. I’ve been using this as a guide ([Run code on exit - Processing Forum](https://forum.processing.org/one/topic/run-code-on-exit.html)). 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.

```auto
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");
    }
  }));
}

```

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [October 4, 2023, 9:22am UTC](https://discourse.processing.org/t/execute-code-when-sketch-stops/42868/2 "2023-10-04T09:22:21Z")

</div>

Hi @garrettlynch,

just like this …

Cheers  
— mnse

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

void draw() {
   background(0);
}

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

```

---

<div class="post-metadata">

**Author:** ![garrettlynch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/garrettlynch/32/19853_2.png) [@garrettlynch](https://discourse.processing.org/u/garrettlynch)\
**Post date:** [October 4, 2023, 11:43am UTC](https://discourse.processing.org/t/execute-code-when-sketch-stops/42868/3 "2023-10-04T11:43:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [October 4, 2023, 12:21pm UTC](https://discourse.processing.org/t/execute-code-when-sketch-stops/42868/4 "2023-10-04T12:21:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![garrettlynch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/garrettlynch/32/19853_2.png) [@garrettlynch](https://discourse.processing.org/u/garrettlynch)\
**Post date:** [October 4, 2023, 3:17pm UTC](https://discourse.processing.org/t/execute-code-when-sketch-stops/42868/5 "2023-10-04T15:17:53Z")

</div>

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.
