What am I doing wrong with ThreadExecutionState?

1

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?

c#
.net
sleep-mode
asked on Stack Overflow Dec 3, 2018 by Kek Lmao • edited Dec 3, 2018 by Kek Lmao

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0