I am struggling to prevent Windows 11 from sleeping.
I have tried every combination of:
ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED
for (;;)
{
SetThreadExecutionState(ES_SYSTEM_REQUIRED);
// SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
// SetThreadExecutionState(ES_AWAYMODE_REQUIRED)
// SetThreadExecutionState(ES_AWAYMODE_REQUIRED | ES_CONTINUOUS);
// SetThreadExecutionState(ES_AWAYMODE_REQUIRED | ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
Thread.Sleep(10000);
Console.WriteLine("The current time is: " + DateTime.Now);
}
private const uint ES_CONTINUOUS = 0x80000000;
private const uint ES_SYSTEM_REQUIRED = 0x00000001;
private const uint ES_DISPLAY_REQUIRED = 0x00000002;
private const uint ES_AWAYMODE_REQUIRED = 0x00000040;
I have tried it inside the for loop and outside the for loop.
Nothing I do stops the system from sleeping after around 3 – 5 minutes.
Is there a different way to do this in Windows 11?