I have to stop windows from going into sleep when my program is running.
And I don’t only want to prevent the sleep-timer, I also want to cancel the sleep-event if I press the sleep-button or in any other way actively tell the computer to sleep. Therefore SetThreadExecutionState is not enough.
Or…I don’t actually have to prevent the sleep completely, only delay it 5-10sec to allow my program to finish a task.
(I know that this is bad program behavior but it’s only for personal use.)
I had a problem like this with a hardware device connected via usb. XP /Vista would sleep/hibernate right in the middle of … Great you say, when it resumes it can just continue. If the hardware is still connected!!! Users have the habit of pulling cables out whenever they feel like it.
You need to handle XP and Vista
Under XP trap the WM_POWERBROADCAST and look for the PBT_APMQUERYSUSPEND wparam.
Under Vista use SetThreadExecutionState like this
and when you app has finished set it back to normal