Posted By: Remon | Jun 23rd @ 11:13 PM
page 1 of 1
Comments: 1 | Views: 1450
Hello,
   I am writing this utility to hook on the windows waiting for a System Shutdown.
So far, I can detect system shutdown methods (Logoff, Restart, Shutdown) by Microsoft.Win32.SystemEvents.SessionEnding event.
However, because Hibernate & Stand by don't send the WM_QUERYENDSESSION, the event above doesn't detect it.
Is there anyway to detect those 2 events & prevent them from happening if the user chooses not to ?
Thanks,
Remon
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
In Windows 2000, XP, and 2003, you can listen for the WM_POWERBROADCAST message which sends the PBT_APMQUERYSUSPEND notification.

However, on Windows Vista, this notification was removed. The reason for this (I guess) is that in many cases hibernate and suspend occur when the user is not in a position to respond to a prompt, e.g. after an automatic timeout or when the user closes the lid of a laptop (in which case he cannot see the screen). If I close my laptop's lid and expect it to go into sleep mode, and your application prevents it from doing so by putting up an "are you sure?" dialog (which I don't see because the lid is closed), and three hours later I find the battery has died, I won't like that.

If your application is a server or something else that requires the PC to be continuously on and you wish to prevent the system from suspending without asking the user, you should use the SetThreadExecutionState function on Vista (note that this function does not prevent the screensaver from starting and also does not prevent suspending if the user explicitly initiates e.g. by closing the lid).

So in essence: on 2000, XP and 2003 you can do what you are proposing, but shouldn't. On Vista and 2008, you can't.

EDIT: Note that Vista still sends a PBT_APMSUSPEND notification, so you can still detect suspension, you just can't do anything about it at that point.
page 1 of 1
Comments: 1 | Views: 1450