程序运行C#时阻止睡眠模式
本文关键字:模式 运行 程序 | 更新日期: 2023-09-27 18:25:30
应用程序在计算机上运行的时间更长,但如果计算机进入睡眠模式,就会出现问题。有什么方法可以防止进入睡眠模式吗?
这样定义class
:
internal static class NativeMethods
{
// Import SetThreadExecutionState Win32 API and necessary flags
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
}
把这个放到主方法:(确保它在application.run
调用之前)
// Set new state to prevent system sleep. (Note: still allows screen saver)
var previousExecutionState = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);