1

I'm trying to clean up resources in my application before it shuts down, following on from my previous question (Detecting When A Java Application Closes) I have implemented the following code which performs the cleanup operation perfectly.

//Intercept when the application closes
            Runtime.getRuntime().addShutdownHook(new Thread()
            {
                @Override
                public void run()
                {
                    //Reclaim resources from MIDI usage
                    if(_midiInstance.CleanUp())
                    {
                        Logger.Add("Closed resources successfully on ShutDown");
                    }
                    else
                    {
                        Logger.Add("Failed to close all resources on ShutDown");
                    }
                    System.exit(0);
                }
            });

Although the System.exit(0); call is understood and processed the application continues to run, just without a visiable GUI. I've thought about placing the System.exit(0) call just outside of the Thread but then it's out of scope, there aren't any other threads or streams running.

Is there an additional step I need to take when hooking in to the ShutDown event to ensure everything closes?

Thanks for your time, I greatly appreciate it.

Community
  • 1
  • 1
Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102

2 Answers2

3

After reading your other question, it seems like your are probably not calling dispose() on your window(s). If true, that would explain the cause of your problem.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • Would I call Dispose() before the call to System.exit(0) ? – Jamie Keeling Apr 20 '11 at 19:15
  • You can call it instead of calling System.exit(0). The application will exit naturally. – Jérôme Verstrynge Apr 20 '11 at 19:23
  • @JVerstry I've tried this.dispose(); but it comes up as a "cannot find" symbol error. I'm using a SwingUI application that extends FrameView, I used the Wizard to make the project and it provided the form for me. – Jamie Keeling Apr 20 '11 at 22:22
  • No, you should try something like e.getWindow().dispose() where e is the WindowEvent received by the windowClosing handler. – Jérôme Verstrynge Apr 20 '11 at 22:41
  • The code sample above shows that I am not able to get access to the WindowsEvent, should I reimplement it using the answer by @John Reasor and do it that way? – Jamie Keeling Apr 20 '11 at 22:51
  • Yes. The listener should be registered on the window itself. – Jérôme Verstrynge Apr 20 '11 at 23:02
  • The only problem I can find is that the instance I use to clean-up resources is in one class (The same class I'm trying to use at the moment), would I have to make my instance static so I can access it from the base class? – Jamie Keeling Apr 20 '11 at 23:07
  • I tried my above solution and it works, I can't access the textBox on the main form anymore but I guess that's the price I have to pay to ensure the resources are released properly. – Jamie Keeling Apr 20 '11 at 23:39
  • In general, it is better to not attach resources to GUI items, unless they are directly related to them. It is like an eco-system of its own. – Jérôme Verstrynge Apr 21 '11 at 11:47
  • It wasn't one of the necessary resources, I was just going to show a little toast to signify it shut down properly. Made use of a log file to do the work instead, thanks JVerstry! – Jamie Keeling Apr 21 '11 at 12:16
1

You need to over ride the windows close button:

            //overriding the windowClosing() method will allow the user to click the close button
    addWindowListener(
            new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    });

By doing this the program will close not just become invisible.

Bit
  • 1,068
  • 1
  • 11
  • 20
  • Yes, but that is a harsh method if work is performed on other threads. It does not give them a chance to clean up nicely. – Jérôme Verstrynge Apr 20 '11 at 19:24
  • I agree dispose() is the better choice over System.exit(0); I was really pointing out the override for windowsClosing – Bit Apr 20 '11 at 19:50