Sic.Framework-Nanjing-Baishi/MECF.Framework.Common/MECF/Framework/Common/Utilities/WIN32APIsWrapper.cs

34 lines
1.2 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace MECF.Framework.Common.Utilities
{
public static class WIN32APIsWrapper
{
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
// Legacy flag, should not be used.
// ES_USER_PRESENT = 0x00000004
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
public static void PreventSleep(bool isDisable = false)
{
if (isDisable)
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
else
// Prevent Idle-to-Sleep (monitor not affected) (see note above)
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_AWAYMODE_REQUIRED |
EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED);
}
}
}