0

Details: I have a JAVA application that takes some time to shutdown. There is a call to close a port, that takes a really long time. I want to add a dialog box that indicates to the user that the application is shutting down. Normally, I would create a dialog box, start a thread to do long work and close dialog, then display the dialog. Once the work is done, the dialog would be close. This does not work for shutting down an application because it seems the window listener closes all windows (kind of makes sense, it supposed to do that). I'm not sure a way around this.

Code:

public void windowClosing(WindowEvent we)
{
    shutDown();
}

public void shutdown()
{
  final JDialog dialog = createDialog();
  Thread t = new Thread
  {
      public void run()
      {
         saveProperties();
         ClosePort();
         dialog.setVisible(false);
         System.exit(0);         
      }
   };
   t.start();
   dialog.setVisible(true);  
}
user2899525
  • 105
  • 10
  • So what happens is that the dialog never appears? – Mordechai Apr 07 '16 at 17:58
  • Possible duplicate of [*How do I close a port in a case of program termination?*](http://stackoverflow.com/q/767292/230513) – trashgod Apr 07 '16 at 18:00
  • Dialog appears, but it disappears immediately once the window listener completes – user2899525 Apr 07 '16 at 18:01
  • @trashgod, thanks for the suggestion! Though, I want to know how to view a dialog. I've seen many applications that indicate the program is shutting down, but how is this down specific to JAVA. – user2899525 Apr 07 '16 at 18:04
  • This [example](http://stackoverflow.com/a/8336218/230513) illustrates `dispatchEvent(WindowEvent.WINDOW_CLOSING)`. – trashgod Apr 07 '16 at 18:30
  • @tashgod, so the issue is not that the JDialog box does not close. The issue is that the dialog closes before it is set invisible. The window listener closes it before hand. – user2899525 Apr 07 '16 at 18:53

1 Answers1

1
t.setDaemon(true);

as daemon threads stay alive even if the rest is gone.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • The thread still runs, however, the GUI gets wiped out. A user thinks the application has completed, however, it is still in the background running. If they open another application before the original closes, this would create issues. – user2899525 Apr 07 '16 at 18:13
  • @user2899525 A valid point. For instance imagine closing the application and then shutting down the system. Maybe saving should be made faster. – Joop Eggen Apr 07 '16 at 18:21