# Detect closing another sketch

**URL:** <https://discourse.processing.org/t/detect-closing-another-sketch/5500>\
**Category:** Coding Questions\
**Created:** [November 13, 2018, 2:31pm UTC](https://discourse.processing.org/t/detect-closing-another-sketch/5500 "2018-11-13T14:31:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)\
**Post date:** [November 13, 2018, 2:31pm UTC](https://discourse.processing.org/t/detect-closing-another-sketch/5500/1 "2018-11-13T14:31:06Z")

</div>

Note that this is outside the PDE, you need vscode / intelliJ / eclipse.

My goal is to start a sketch, run that, and when it is closed either by me or some timer or whatever, run another sketch.  
For a sketch to run I create a object of it first.  
This is wrong cause it is supposed to be called static but then I will have no reference.

After that I wait in the while loop to get the PSurface, but it seems this always stays null for some reason.

This is what I have now:

```auto
  public static void main(String[] args) {
  
        SomeSketch c = new SomeSketch(); 
        c.main(args);

        PSurface s = null;
        
        while (s == null) {
            s = c.getSurface();
            try {
                Thread.currentThread().sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        println("!");
    }

```

Running the sketch goes fine but I can never detect it being closed, or even get into the while loop.

I hope someone can help.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [November 13, 2018, 5:44pm UTC](https://discourse.processing.org/t/detect-closing-another-sketch/5500/2 "2018-11-13T17:44:39Z")

</div>

Hi, I haven’t tried this yet, but Java 9 improved a lot the Process API. See [https://www.javaworld.com/article/3176874/java-language/java-9s-other-new-enhancements-part-3.html](https://www.javaworld.com/article/3176874/java-language/java-9s-other-new-enhancements-part-3.html)  
At the end it mentions “Triggering actions on process termination”. Would this help you?
