Skip to content

Instantly share code, notes, and snippets.

@brianhassel
Last active July 4, 2025 11:14

Revisions

  1. brianhassel renamed this gist Aug 21, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. brianhassel created this gist Aug 21, 2015.
    21 changes: 21 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    internal static class NativeMethods {

    public static void PreventSleep() {
    SetThreadExecutionState(ExecutionState.EsContinuous | ExecutionState.EsSystemRequired);
    }

    public static void AllowSleep() {
    SetThreadExecutionState(ExecutionState.EsContinuous);
    }

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

    [FlagsAttribute]
    private enum ExecutionState : uint {
    EsAwaymodeRequired = 0x00000040,
    EsContinuous = 0x80000000,
    EsDisplayRequired = 0x00000002,
    EsSystemRequired = 0x00000001
    }
    }