49

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?

1
  • 7
    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. Commented 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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

on some Windows XP machines of our clients, the NativeMethods.SetThreadExecutionState never returns 0, any idea?
@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)
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.
Could be me but this doesnt seem t work in Windows 10
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.
|
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);

Comments

11

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:

Comments

1

i know this is old, mine is a console program

 class Program
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern uint SetThreadExecutionState(EXECUTION_STATE esFlags);
        public const uint ES_CONTINUOUS = 0x80000000;
        public const uint ES_SYSTEM_REQUIRED = 0x00000001;




####then call this below code

  public static void keepalive()
        {
            int x = 0;
            for(; ; )
            {
                x++;
                SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
                Console.WriteLine("Preventing lock screen.... disabling proxy");
              
                    setProxy("", false);
                   
             
                Thread.Sleep(10000);
                if (x == 10)
                {
                    x = 0;
                    Console.Clear();
                }
            }
            
        }

2 Comments

using System; using System.Runtime.InteropServices; using System.Threading; using Microsoft.Win32;
Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including at least one that has been validated by the community. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.