I'm trying to prevent my laptop from going to sleep when I'm opening a program, I've set the sleep mode to 1 minute and am running a timer set to tick every 50 seconds.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
timer1.Start();
}
[FlagsAttribute]
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
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
private void timer1_Tick(object sender, EventArgs e)
{
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED);
}
}
When running this program it waits for 50 seconds, should fire the function and prevent it from sleeping, yet it goes to sleep ~50 seconds after starting the program. what am I doing wrong?
User contributions licensed under CC BY-SA 3.0