Sean Blakemore's Blog

Like trying to fit a square peg in a round hole

18. February 2012 13:23
by Sean

Power Management in C# – Stop the system or display from sleeping

18. February 2012 13:23 by Sean | 0 Comments

In a desktop application I’m working on there are a number of long running batch processes that can be run by the user. This caused problems when the user kicked off a batch process and then walked away from the computer, returning a few hours later to find that the system had gone to sleep and the batch had failed!


A little searching led me to the unmanaged SetThreadExecutionState function which can be used to tell Windows that the calling thread is doing something important which requires the system to be awake. The function can be used in two ways, it can be called repeatedly to reset the system’s idle timer or it can be used in “continuous” mode where the system should not enter sleep mode until told otherwise. Check the MSDN link above for full details.