Question

Well behaved windows programs need to allow users to save their work when they are shutting the PC down.

How can I make my app detect the shutdown event? Any solution should allow the user to abort the shutdown if user selects, say "Cancel".

The normal Swing window closing hook doesn't work, nor does adding a shutdown hook.

On testing, the methods of WindowListener (windowClosing,windowClosed, etc) do not get called.

The answer I have accepted requires the use of platform specific code (JNI to register for WM_QUERYENDSESSION ). Isn't this a bug on Swing?

See http://forums.sun.com/thread.jspa?threadID=481807&messageID=2246870

Was it helpful?

Solution

Write some JNI code to WM_QUERYENDSESSION message. You can get details for this from the MSDN documentation or by googling it.

If you don't want to write too much C++ code to do this I can recommend the JNA library click here. Which gives you some nice Java abstractions for C code.

OTHER TIPS

The above seems to be the better answer.

I can't find any good information on detecting window shutdown events. I guess the best possible method would be to detect weather your application is trying to close, using a window closing event or the like then ask the question.

http://www.javalobby.org/java/forums/t17933

Look for signal handling in java. when Windows closes it will send a signal to the application asking it to terminate most likely a sigterm

see here for more about this (I am not the owner of the website)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top