Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   PC shutdown event...? (http://www.programmingforums.org/showthread.php?t=11898)

AntiNinja Nov 15th, 2006 8:15 PM

PC shutdown event...?
 
So I was writing a small app and was wondering what to do if the user presses the power button on their pc whilst the app is still running? I read an article that prevents Standby/Hibernate, but not shutdown...Is it possible to do anything like this in Java?

(Like if I needed to write to a file to save settings and whatnot.)

ReggaetonKing Nov 15th, 2006 9:45 PM

The java.util.prefs were introduced in Java 1.4. This package is used to write/read files about your application. Such as saving the window's previous location or the size of the window.

I would recommend, every time a user triggers an event, I would record the information.

Here a link to help you get started: http://javaalmanac.com/egs/java.util.prefs/pkg.html

Dameon Nov 16th, 2006 5:23 PM

Off the top of my head, all of your forms/frames/windows will get the WM_CLOSE message on any logout or shutdown event. The actual shutdown does not occur until all windows close, gracefully or otherwise.

DaWei Nov 16th, 2006 6:32 PM

Actually, I'm not so sure about that. When I was experimenting with drastic shutdowns at the Win API level, the documentation indicated that I had about 20 ms (a relatively long time if one's process doesn't get swapped out) to wrap it all up. You're probably correct, however, if the adjective "otherwise" indicates that one may be dumped in the landfill before finishing the cleanup.

Harakim Nov 30th, 2006 4:56 AM

Windows will stay open until all of the programs close. I have had it hang and run my battery out on a few occasions because a GDK+ or GDI+ or some kind of window won't close for 6 hours.

grumpy Nov 30th, 2006 5:57 AM

Not sure about the java aspects, but the following normally happens after a windows shutdown of logoff is initiated.

1) All windows applications will receive a WM_QUERYENDSESSION message asking for permission to shutdown. An application can indicate, by setting an appropriate flag, that it does want to allow the shutdown/logoff.

2) All windows applications will receive a WM_ENDSESSION message, with a value indicating whether the shutdown is proceeding or not. If shutdown or logoff is occurring, that flag is TRUE.

3) There are a set of similar signals sent to each console process (eg the DOS prompt), to make them shut down as well. I've never developed a console application that needed to respond to a windows shutdown event, so can't give more details.

4) If shutdown is happening, each program is terminated (the sequence of events such as WM_CLOSE for windows applications). This allows the applications to clean up as they terminate (including doing things like prompting a user to save files, which is why a shutdown will not occur while a program waits for user input).


The above is what happens in response to a user manually initiates a shutdown or logs off. It can also be initiated programatically with a call to the win32 API functions ExitWindows() or ExitWindowsEx(). ExitWindowsEx() accepts a flag that allows bypassing of the protocol of asking programs for permission to shutdown in emergency situations (it basically shuts down applications with prejudice, and does not allow data to be saved) --- that can't be done manually. Such a technique is one that should be used with extreme caution, as it can make your program unpopular with users.


All times are GMT -5. The time now is 10:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC