I have a service application that runs in conjunction with a user-mode module. The software allows to perform a specialized power operation at which the user-mode code first logs ff the Windows user and right after that the service application puts system into sleep mode.

This scenario works fine on some systems, but on others the sleep operation fails. At times the sleep succeeds, but then when I wake up the system and try to perform it again, the sleep API (SetSuspendState) hangs up and never returns, while the system remains operational.

While doing tests, I put a hard-coded delay (with a Sleep(5000) API) that seemed to resolve the issue on most systems, but I'm still getting the same result on others. In this case, I'd hate introducing a longer delay since it doesn't really solve the issue, or answers the question why it was happening in the first place. Plus, I'm sure there's a "slow" system out there for which, even 10 seconds will not be enough...

So, does anyone have any idea how to fix these failures in the sleep API?

PS. I run my tests on the latest version of Windows 7.