48

I am working on a C# application which runs in the background without any Windows control.

I want to notify Windows that my application is still alive to prevent Windows from going into the idle state.

Are there any APIs available to call from my application which notify the Windows OS that my application is still alive?

Thanks in advance.

1
  • 6
    Be sure to let your users somehow know that the application needs to be run continuously and that it prevents Windows from going idle. This is especially important for users with laptops.
    – In silico
    Jun 10, 2011 at 5:31

5 Answers 5

44

You've to use SetThreadExecutionState function. Something like this:

public partial class MyWinForm: Window
{
    private uint fPreviousExecutionState;

    public Window1()
    {
        InitializeComponent();

        // Set new state to prevent system sleep
        fPreviousExecutionState = NativeMethods.SetThreadExecutionState(
            NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
        if (fPreviousExecutionState == 0)
        {
            Console.WriteLine("SetThreadExecutionState failed. Do something here...");
            Close();
        }
    }

    protected override void OnClosed(System.EventArgs e)
    {
        base.OnClosed(e);

        // Restore previous state
        if (NativeMethods.SetThreadExecutionState(fPreviousExecutionState) == 0)
        {
            // No way to recover; already exiting
        }
    }
}

internal static class NativeMethods
{
    // Import SetThreadExecutionState Win32 API and necessary flags
    [DllImport("kernel32.dll")]
    public static extern uint SetThreadExecutionState(uint esFlags);
    public const uint ES_CONTINUOUS = 0x80000000;
    public const uint ES_SYSTEM_REQUIRED = 0x00000001;
}
7
  • on some Windows XP machines of our clients, the NativeMethods.SetThreadExecutionState never returns 0, any idea?
    – Alessio
    Mar 23, 2015 at 11:52
  • 5
    @Alessio - a return of 0 means the method failed. From the documentation of SetThreadExecutionState - "If the function succeeds, the return value is the previous thread execution state. If the function fails, the return value is NULL." (Note - I am assuming you probably already had figured that out, but wanted to post an answer for completeness and to help anyone else who might come along and view your comment)
    – pstrjds
    Jun 14, 2016 at 9:36
  • 1
    In trying to resolve a recurring and hard to resolve sleep issue with a Surface Pro 3 running windows 8.1, I can state with clarity that this solution unfortunately does not work for this issue on this OS.
    – Redgum
    Aug 24, 2016 at 23:50
  • 3
    Could be me but this doesnt seem t work in Windows 10
    – ecklerpa
    Dec 6, 2017 at 19:27
  • 2
    Worked for me in Win 10 @ecklerpa. Also, if anyone needs to do this for a visible appliaction, also remember to set ES_DISPLAY_REQUIRED to avoid the display turning off. Mar 8, 2018 at 7:23
22

You have a couple of options:

  • Use SetThreadExecutionState, which:

    Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.

    Where you could use the ES_SYSTEM_REQUIRED flag to

    Forces the system to be in the working state by resetting the system idle timer.

  • Use SendInput to fake keystroke, mouse motion/clicks

  • Another alternative would be to change your app to be a Windows service.

SetThreadExecutionState example

// Television recording is beginning. Enable away mode and prevent
// the sleep idle time-out.
SetThreadExecutionState(
    ES_CONTINUOUS | 
    ES_SYSTEM_REQUIRED | 
    ES_AWAYMODE_REQUIRED);

// Wait until recording is complete...
// Clear EXECUTION_STATE flags to disable away mode and allow the system
// to idle to sleep normally.
SetThreadExecutionState(ES_CONTINUOUS);
10

You can use SetThreadExecutionState described here:

Since it is a Win32 API function, to use it from C# you'll need to PInvoke it. The steps are described here, including a sample method PreventSleep to temporarily disable sleep mode:

0
0

I don't think there's any way to do this directly in managed code.

A quick search reveals this post from 2 years ago. Basically you'd need to do some interop to call a raw windows API.

-3

Here is SetThreadExecutionState C# implementation

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.