# Callback when window has been closed by user

**URL:** <https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125>\
**Category:** How to?\
**Created:** [June 23, 2020, 8:58am UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125 "2020-06-23T08:58:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![andremichelle](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/andremichelle/32/145_2.png) [@andremichelle](https://openrndr.discourse.group/u/andremichelle)\
**Post date:** [June 23, 2020, 8:58am UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/1 "2020-06-23T08:58:42Z")

</div>

I have an application running that plays an audio file. I am using _net.beadsproject.beads_ to get an audio-environment. If I close the window, the audio-playback is still running, since it runs in a different thread. I have to kill the processes in Intellij. Is there a callback I can use, when the window has been closed to manually stop the audio-thread?

---

<div class="post-metadata">

**Author:** ![abe](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/abe/32/700_2.png) [@abe](https://openrndr.discourse.group/u/abe)\
**Post date:** [June 24, 2020, 1:11pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/2 "2020-06-24T13:11:45Z")

</div>

Hi! Welcome to the forum 🙂

I see `Program.kt` has these window events:

```auto
enum class WindowEventType {
    MOVED,
    RESIZED,
    FOCUSED,
    UNFOCUSED,
    MINIMIZED,
    RESTORED,
}

```

Could a `CLOSE` event be added? @edwin

It sounds useful to be able to clean things up…

---

<div class="post-metadata">

**Author:** ![edwin](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@edwin](https://openrndr.discourse.group/u/edwin)\
**Post date:** [June 24, 2020, 4:48pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/3 "2020-06-24T16:48:56Z")

</div>

Ah yeah. That’s a good one. Until that’s there you can use JVM shutdown hooks. For example: [https://github.com/openrndr/openrndr/blob/master/openrndr-openal/src/main/kotlin/AudioPlayer.kt#L23](https://github.com/openrndr/openrndr/blob/master/openrndr-openal/src/main/kotlin/AudioPlayer.kt#L23)

---

<div class="post-metadata">

**Author:** ![andremichelle](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/andremichelle/32/145_2.png) [@andremichelle](https://openrndr.discourse.group/u/andremichelle)\
**Post date:** [June 24, 2020, 6:46pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/4 "2020-06-24T18:46:11Z")

</div>

```
    init {
    samplePlayer = SamplePlayer(context, SampleManager.sample(samplePath))
    context.out.addInput(samplePlayer)

    Runtime.getRuntime().addShutdownHook(object : Thread() {
        override fun run() {
            println("shutdown")
            samplePlayer.kill()
            context.stop()
        }
    })
}

```

If I close the window (CMD+Q Mac), the callback will not be fired. Only if I quit the process in Intellij.

---

<div class="post-metadata">

**Author:** ![edwin](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@edwin](https://openrndr.discourse.group/u/edwin)\
**Post date:** [June 25, 2020, 5:04pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/5 "2020-06-25T17:04:49Z")

</div>

Strange, can you double check that? JVM shutdown hooks work with all modes of termination on all platforms for me.

---

<div class="post-metadata">

**Author:** ![andremichelle](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/andremichelle/32/145_2.png) [@andremichelle](https://openrndr.discourse.group/u/andremichelle)\
**Post date:** [June 25, 2020, 6:10pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/6 "2020-06-25T18:10:11Z")

</div>

Confirmed. I am constantly working on the project and I always have to stop the process in Intellij. The code snippets is still in there, but the console only logs “shutdown” when stopping the process manually.

---

<div class="post-metadata">

**Author:** ![edwin](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@edwin](https://openrndr.discourse.group/u/edwin)\
**Post date:** [June 25, 2020, 8:12pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/7 "2020-06-25T20:12:51Z")

</div>

That sounds like there may be a non-daemon thread running that prevents the JVM from shutdown.

---

<div class="post-metadata">

**Author:** ![andremichelle](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/andremichelle/32/145_2.png) [@andremichelle](https://openrndr.discourse.group/u/andremichelle)\
**Post date:** [June 26, 2020, 4:47am UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/8 "2020-06-26T04:47:15Z")

</div>

Maybe it is connected to the Driver I am using.

```
private val context: AudioContext = AudioContext(
    JavaSoundAudioIO(512),
    512,
    IOAudioFormat(44100.0f, 16, 0, 2)
)

```

I am happy to try any other option to play wav-files in openrndr.

---

<div class="post-metadata">

**Author:** ![ricardo](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/ricardo/32/8_2.png) [@ricardo](https://openrndr.discourse.group/u/ricardo)\
**Post date:** [June 29, 2020, 3:35pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/9 "2020-06-29T15:35:23Z")

</div>

The shutdown hook never runs, because it’s waiting for the sample player thread to “finish”. I don’t think there’s a way around it till there’s a `window closed` life-cycle event.

---

<div class="post-metadata">

**Author:** ![edwin](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@edwin](https://openrndr.discourse.group/u/edwin)\
**Post date:** [June 30, 2020, 6:57pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/10 "2020-06-30T18:57:19Z")

</div>

I made Beads play sound using OPENRNDR’s OpenAL sound interface a good while ago. Sort of an abandoned codebase: [https://github.com/edwinRNDR/pellet/blob/master/src/main/kotlin/org/openrndr/pellet/Pellet.kt](https://github.com/edwinRNDR/pellet/blob/master/src/main/kotlin/org/openrndr/pellet/Pellet.kt)

---

<div class="post-metadata">

**Author:** ![edwin](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@edwin](https://openrndr.discourse.group/u/edwin)\
**Post date:** [June 30, 2020, 9:23pm UTC](https://openrndr.discourse.group/t/callback-when-window-has-been-closed-by-user/125/11 "2020-06-30T21:23:55Z")

</div>

I just submitted code that addresses [https://github.com/openrndr/openrndr/issues/126](https://github.com/openrndr/openrndr/issues/126)
