> ----------
From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of xxxxx@microsoft.com[SMTP:xxxxx@microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Friday, March 09, 2007 6:30 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to detect system suspend?
Here are a couple of answers/comments for this post:
(1) Yes, the MSDN documentation is wrong. SERVICE_CONTROL_POWEREVENT does indeed take the other power types (e.g., PBT_APMSUSPEND, PBT_APMRESUMEAUTOMATIC, etc.) and not just PBT_POWERSETTINGCHANGE. This was a new setting added in Vista, and it looks like there was a mistake in editing the MSDN docs for this page. I’ll look into geting the MSDN help updated correctly.
Great, thanks.
(2) In addition to not scaling well, using global named events for indicating power state transitions has one big problem: how would the OS “know” that listeners of these events have finished whatever work they needed to do when the event was signalled? Using directed window messages and service notifications, we do know when you’re done and that’s it’s safe to move on.
I didn’t mention to replace current solution with global named events. Instead, add more options. Some application just need to be notified about power state transitions and don’t need a handshake with OS. Notification events are ideal for this purpose; simple and efficient. App can both wait for them or just read the current state.
BTW, the handshake could be implemented using two named events but it is rather inelegant solution.
(3) I’m not sure how a Win32 API to query the current power state of the system would be of any use, plus it would require a polling infrastructure (which should almost always be avoided). What you really want is a notification that the state is changing “right now” and a guarnateed handshake mechanim with the OS where you have a chance to take some action (which is what we have).
What *I* really want is a way how to block app until system awakes from suspend. And a way how to know if the system was suspended since last query. We currently implemented a temporary solution for a service using one notification event and one counter. Service notification handler sets the event when system is in S0 and resets when leaves it and increments the counter when system is leaving S0. Clients (the library) can wait for the event and check the counter when necessary.
(4) Yes, notifications in consoles is painful. We fully understand this and are looking a ways of making this easier in future versions of Windows (e.g., don’t need to spin up extra threads, create hidden windows to receive messages, etc.).
It would be nice. Please consider that code which needs notification may need to be separated from the app/service. In our case we provide a DLL with an API allowing to communicate unified way with our different hardware. The DLL should be independent piece of code and shouldn’t need anything from the caller and shouldn’t influence behaviour of the process which uses it. It should also work from any process the same way. Current notifications don’t fit these requirements even for standard app. It is ugly when library creates a hidden window and thread even for app with windows. Yes, we’re doing it but currently it stopped work with services.
I guess the ideal solution would be some kind of callbacks which’d code register and which wouldn’t influence anything else. Similarly as in kernel when a driver can register power state and power settings change callbacks.
Best regards,
Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]