Code Supposed to Run on Exit does not work when Exported

I have written a Processing sketch that is supposed to run certain code upon exiting (see here). Whenever the program is run in Processing, the shutdown works, but whenever I export the program to run on Windows without Processing, the shutdown code is never executed. What is a possible solution to this problem?

Answers

  • 1st step is write a bare minimum code showing your bug in action! ~:>

  • edited June 2014

    Here's a simple as the code can get to demonstrate the problem:

    public class ExitHandler
    {
      ExitHandler(PApplet thisProgram)
      {
        thisProgram.registerMethod("dispose", this);
      }
    
      public void dispose()
      {
        File randomFile = new File("randomFolder"); // When running the program in Processing, make this an absolute file path.
        randomFile.mkdir();
      }
    }
    

    This code, when run in Processing , creates a file named "randomFolder" whenever it is closed. In order for this to be demonstrated in Processing, you should make the randomFile variable an actual file path because having the file generate in the executing folder when in Processing is very strange.

    Whenever I tested this code, I had the folder generate on my desktop. Without making changes to the code and exporting for Windows, I ran the program again and the folder did not generate.

    I am sorry that the code is messed up in the post. You might find the code easier to read here.

  • I am sorry that the code is messed up in the post.

    Highlight the code block and hit CTRL+K! ;)

  • I'm in the dark too! It was supposed to just work!
    Gonna paste below the methods tasked to deal w/ the exit process:

    https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L4091

    public void exit() {
      if (thread == null) {
        // exit immediately, dispose() has already been called,
        // meaning that the main thread has long since exited
        exitActual();
      } 
      else if (looping) {
        // dispose() will be called as the thread exits
        finished = true;
        // tell the code to call exit2() to do a System.exit()
        // once the next draw() has completed
        exitCalled = true;
      } 
      else if (!looping) {
        // if not looping, shut down things explicitly,
        // because the main thread will be sleeping
        dispose();
    
        // now get out
        exitActual();
      }
    }
    
    /**
     * Some subclasses (I'm looking at you, processing.py) might wish to do something
     * other than actually terminate the JVM. This gives them a chance to do whatever
     * they have in mind when cleaning up.
     */
    protected void exitActual() {
      try {
        System.exit(0);
      } 
      catch (SecurityException e) {
        // don't care about applet security exceptions
      }
    }
    
    
    /**
     * Called to dispose of resources and shut down the sketch.
     * Destroys the thread, dispose the renderer,and notify listeners.
     * <p>
     * Not to be called or overriden by users. If called multiple times,
     * will only notify listeners once. Register a dispose listener instead.
     */
    public void dispose() {
      // moved here from stop()
      finished = true;  // let the sketch know it is shut down time
    
        // don't run the disposers twice
      if (thread != null) {
        thread = null;
    
        // shut down renderer
        if (g != null) {
          g.dispose();
        }
        // run dispose() methods registered by libraries
        handleMethods("dispose");
      }
    }
    
  • I still have not found a good solution for this just yet. I'll be sure to let you know if I do.

  • Answer ✓

    You might also look into adding a shutdown hook using the Runtime.addShutdownHook() method.

Sign In or Register to comment.