[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
public enum EXECUTION_STATE :uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
// Legacy flag, should not be used.
// ES_USER_PRESENT = 0x00000004
}
There is no need to store the state you set, Windows remembers it for you. Just set it back to ES_CONTINUOUS when you don't want it anymore.
Also note that this setting is per thread/application not global, so if you go to ES_CONTINUOUS and another app/thread is still setting ES_DISPLAY the display will be kept on.
Note that the return value is the EXECUTION_STATE that ''was'' set.
Description of what the different EXECUTION_STATE does
http://msdn.microsoft.com/en-us/library/aa373208(v=vs.85).aspx
void PreventMonitorPowerdown ()
{
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
}
void AllowMonitorPowerdown ()
{
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
}
void PreventSleep ()
{
// Prevent Idle-to-Sleep (monitor not affected)
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_AWAYMODE_REQUIRED);
}
void KeepSystemAwake ()
{
SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED);
}
Do you know one? Please contribute it!