Skip to content

Instantly share code, notes, and snippets.

@leandroribeiro
Created August 13, 2021 14:52
Show Gist options
  • Save leandroribeiro/cf47fa877f89c45801b1626fb9ec1851 to your computer and use it in GitHub Desktop.
Save leandroribeiro/cf47fa877f89c45801b1626fb9ec1851 to your computer and use it in GitHub Desktop.
C# - Prevent Sleep on Windows v2
using System;
using System.Threading;
using System.Runtime.InteropServices;
// reference http://eddiejackson.net/lab/2020/03/02/c-prevent-sleep-on-windows-v2/
namespace SleepControl
{
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;
// Initialize array strings
public static string array = "";
public static string userInput = "";
public static string userOption = "";
static void Main(string[] args)
{
// Console Window management
ControlConsole();
// return basic usage message
UseMessage();
// load input
foreach (string s in args)
{
for (int i = 0; i < args.Length; i++)
{
array = args[i].ToString();
}
userInput = userInput + s.ToString() + " ";
}
// manage data entry
string [] argInputs = userInput.Split(' ');
userOption = argInputs[0].ToString(); // retrieve only first option
userOption = userOption.ToLower();
Console.WriteLine("Option: " + userOption);
// perform action logic
AppAction();
}
static void ControlConsole() {
Console.Title = "Sleep Control v2";
Console.Clear();
ShowWindow(ThisConsole, MAXIMIZE);
}
static void UseMessage()
{
Console.WriteLine("\nSleepControl.exe { prevent | allow }\n");
}
static void AppAction() {
if (userOption == "prevent")
{
// 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);
Console.WriteLine("Preventing Sleep!");
}
else if (userOption == "allow")
{
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
Console.WriteLine("Allowing Sleep!");
}
else
{
Console.WriteLine("Option not detected!");
}
Thread.Sleep(3000);
}
}
}
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment