Why does this shutdownhook not work?

This is my main method and it contains shutdownhook:

public static void main(String args[]) { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { JOptionPane.showMessageDialog(null, "Shutdown hook"); } }); /* Create and display the form */ java.awt.EventQueue.invokeLater( new Runnable() { @Override public void run() { Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); MyFrame frame = new MyFrame(); frame.setVisible(true); } }); } 

The problem is that JOptionPane not showing at all. Instead, the frame closes, but the application itself still works. PS. I cannot use the WindowClosing event because it does not fire on the Cmd + Q command on Mac OS X.

+4
source share
1 answer

The thread you are sending is already disabled, or most likely will be disabled before your queue is sent.

You need to find another way to catch exit events, you can disable the hook so that you can clear when the VM exits, so you really don't want to bind more resources in the user interface when it is called.

+1
source

All Articles