3

I am trying to implement a windows service that sends a message when the system goes into standby mode (sleep). My test machine OS is a Windows 8 pro. The service has CanHandlePowerEvents=true and OnPowerEvent overwritten and it indeed receives power events.

The issue I encountered is: OnPowerEvent never receives QuerySuspend message. It does however receive Suspend message before computer goes into suspend but the OS doesn't wait for OnPowerEvent method to complete, it just goes into suspend mode regardless, then after resume, the execution of the OnPowerEvent method continues.

Thing is sending a message over internet may take 1-2 seconds so it always ends up being sent after the computer resumes and never before going into suspend mode as it is supposed to work.

Any ideeas on how to solve this, anyone?

  • OnPowerEvent runs asynchronously anyway, there isn't anything you can do to delay the shutdown. Scratch the idea, you can't make it solid. – Hans Passant Dec 22 '13 at 17:40

1 Answers1

1

In Vista and newer, applications have two seconds to complete any action triggered by a PBT_APMSUSPEND event. This setting is not configurable.

When an application receives notification that the system is about to enter sleep, it should perform any necessary operations quickly and return out of the message loop. The system allows for a maximum of two seconds per application when handling this message before timing out.

http://msdn.microsoft.com/en-us/library/aa373223%28v=vs.85%29.aspx

http://blogs.msdn.com/b/oldnewthing/archive/2011/11/24/10241125.aspx