Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

setthreadexecutionstate (kernel32)
 
.
Summary
SetThreadExecutionState is used to stop the machine timing out and entering standby/switching the display device off.

C# Signature:

[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true))]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

User-Defined Types:

FlagsAttribute

public enum EXECUTION_STATE :uint
{
   ES_SYSTEM_REQUIRED  = 0x00000001,
   ES_DISPLAY_REQUIRED = 0x00000002,
   // Legacy flag, should not be used.
   // ES_USER_PRESENT   = 0x00000004,
   ES_CONTINUOUS       = 0x80000000,
}

Notes:

There is no need to store the state you set, Windows remembers it for you. Just set it back to ES_CONTINUOUS when you don't want it anymore.

Also note that this setting is per thread/application not global, so if you go to ES_CONTINUOUS and another app/thread is still setting ES_DISPLAY the   display will be kept on.

Tips & Tricks:

Please add some!

Sample Code:

    void PreventMonitorPowerdown ()
    {
        SetExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED |
                            EXECUTION_STATE.ES_CONTINUOUS);
    }

    void AllowMonitorPowerdown ()
    {
        SetExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions