Last active
August 13, 2021 14:53
-
-
Save leandroribeiro/bf509991a604dbbd39ca983464d21518 to your computer and use it in GitHub Desktop.
C# - Prevent Sleep on Windows v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
namespace DisableSleepMode | |
{ | |
class Program | |
{ | |
// Sleep Control | |
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] | |
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); | |
[FlagsAttribute] | |
public enum EXECUTION_STATE : uint | |
{ | |
ES_AWAYMODE_REQUIRED = 0x00000040, | |
ES_CONTINUOUS = 0x80000000, | |
ES_DISPLAY_REQUIRED = 0x00000002, | |
ES_SYSTEM_REQUIRED = 0x00000001 | |
} | |
// Console Window Handling | |
[DllImport("kernel32.dll", ExactSpelling = true)] | |
private static extern IntPtr GetConsoleWindow(); | |
private static IntPtr ThisConsole = GetConsoleWindow(); | |
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] | |
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); | |
private const int HIDE = 0; | |
private const int MAXIMIZE = 3; | |
private const int MINIMIZE = 6; | |
private const int RESTORE = 9; | |
static void Main(string[] args) | |
{ | |
ShowWindow(ThisConsole, MINIMIZE); | |
Console.WriteLine("Preventing Sleep!"); | |
// types of sleep | |
// disable monitor sleep | |
// SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS; | |
// disable monitor sleep and keep system awake | |
// SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED); | |
// disable monitor sleep and keep system awake and prevent idle to sleep | |
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED); | |
// wait for user | |
Console.WriteLine("\npress any key to continue...\n"); | |
Console.ReadKey(); | |
// allow sleep | |
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS); | |
} | |
} | |
} | |
//reference http://eddiejackson.net/lab/2020/02/28/c-prevent-sleep-on-windows/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment