# Save when app close, exit() ... stop()

**URL:** <https://discourse.processing.org/t/save-when-app-close-exit-stop/25754>\
**Category:** Coding Questions\
**Created:** [November 27, 2020, 11:22am UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754 "2020-11-27T11:22:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [November 27, 2020, 11:22am UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754/1 "2020-11-27T11:22:12Z")

</div>

Hi, I’m trying to save data when my app close either with mouse or ESC button.  
I try with exit() and stop() with any result.  
this is my code

```auto
void stop(){
    saveSlides();
    exit();
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [November 27, 2020, 1:24pm UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754/2 "2020-11-27T13:24:53Z")

</div>

I think stop() is deprecated

have a look at [addShutdownHook()](https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html)

try adding this in the setup()

```auto
  // called upon program termination
  Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {   
      saveSlides();
    }
  }
  );

```

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [November 27, 2020, 3:44pm UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754/3 "2020-11-27T15:44:07Z")

</div>

Thank you a lot. It works perfect

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [November 27, 2020, 3:55pm UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754/4 "2020-11-27T15:55:49Z")

</div>

great! glad it helped

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [November 28, 2020, 11:29am UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754/5 "2020-11-28T11:29:13Z")

</div>

The Processing method your looking for is `dispose()`:

```auto
@Override
public void dispose() {
	println("Print this when sketch is closed.");
}

```

It seems as if `stop()` if called only when an applet runs in a web browser (which isn’t possible anymore anyway!)

```auto
Called by the browser or applet viewer to informthis applet that it should stop its execution. 
Unfortunately, there are no guarantees from the Java specwhen or if stop() will be called (i.e. on browser quit,or when moving between web pages), and it's not always called.

```

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [November 28, 2020, 2:59pm UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754/6 "2020-11-28T14:59:33Z")

</div>

I tried with dispose() but when I click on the cross the function run but the sketch doesn’t close. Thanks for help

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [November 28, 2020, 3:53pm UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754/7 "2020-11-28T15:53:17Z")

</div>

Interesting. Try this then:

```auto
public void dispose() {
	println("Print this when sketch is closed.");
    super.dispose();
}

```

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [November 29, 2020, 12:25pm UTC](https://discourse.processing.org/t/save-when-app-close-exit-stop/25754/8 "2020-11-29T12:25:32Z")

</div>

Now it’s ok, great !
