程序运行时防止窗口进入睡眠状态?

当我的程序运行时,我不得不停止窗口进入睡眠状态。

而且我不仅要防止睡眠计时器,还要取消睡眠事件,如果按下睡眠button或以任何其他方式主动告诉计算机hibernate。 因此SetThreadExecutionState是不够的。

或者…我实际上并不需要完全阻止睡眠,只是延迟5-10秒才能让我的程序完成任务。

(我知道这是坏的程序行为,但它只是为了个人使用。)

Solutions Collecting From Web of "程序运行时防止窗口进入睡眠状态?"

我有一个这样的问题,通过USB连接硬件设备。 XP / Vista会睡在/休眠正好在… …你说的很好,当它恢复时,它可以继续。 如果硬件仍然连接! 用户习惯于随时随地拉出电缆。

你需要处理XP和Vista

在XP下捕获WM_POWERBROADCAST并查找PBT_APMQUERYSUSPEND wparam。

// See if bit 1 is set, this means that you can send a deny while we are busy if (message.LParam & 0x1) { // send the deny message return BROADCAST_QUERY_DENY; } // if else { return TRUE; } // else 

在Vista下使用像这样的SetThreadExecutionState

 // try this for vista, it will fail on XP if (SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED) == NULL) { // try XP variant as well just to make sure SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED); } // if 

当你的应用程序已经完成设置恢复正​​常

 // set state back to normal SetThreadExecutionState(ES_CONTINUOUS); 

在考虑vim的答案后

“使用PowerCreateRequest,PowerSetRequest和PowerClearRequest函数是首选的方法。”

与msdn上的链接的AvailabilityRequests.docx,这是耗尽进入它(太多阅读),我已经在网上搜索一个具体的例子在C#是基于PowerCreateRequest,并找到http://go4answers.webhost4life.com /Example/problem-monitor-wakeup-service-windows7-12092.aspx [编辑2016 – 不再可用]

复制并调整它以满足我的需求(从msdn复制的CloseHandle的PInvoke):

 using System.Runtime.InteropServices; #region prevent screensaver, display dimming and automatically sleeping POWER_REQUEST_CONTEXT _PowerRequestContext; IntPtr _PowerRequest; //HANDLE // Availability Request Functions [DllImport("kernel32.dll")] static extern IntPtr PowerCreateRequest(ref POWER_REQUEST_CONTEXT Context); [DllImport("kernel32.dll")] static extern bool PowerSetRequest(IntPtr PowerRequestHandle, PowerRequestType RequestType); [DllImport("kernel32.dll")] static extern bool PowerClearRequest(IntPtr PowerRequestHandle, PowerRequestType RequestType); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] internal static extern int CloseHandle(IntPtr hObject); // Availablity Request Enumerations and Constants enum PowerRequestType { PowerRequestDisplayRequired = 0, PowerRequestSystemRequired, PowerRequestAwayModeRequired, PowerRequestMaximum } const int POWER_REQUEST_CONTEXT_VERSION = 0; const int POWER_REQUEST_CONTEXT_SIMPLE_STRING = 0x1; const int POWER_REQUEST_CONTEXT_DETAILED_STRING = 0x2; // Availablity Request Structures // Note: Windows defines the POWER_REQUEST_CONTEXT structure with an // internal union of SimpleReasonString and Detailed information. // To avoid runtime interop issues, this version of // POWER_REQUEST_CONTEXT only supports SimpleReasonString. // To use the detailed information, // define the PowerCreateRequest function with the first // parameter of type POWER_REQUEST_CONTEXT_DETAILED. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct POWER_REQUEST_CONTEXT { public UInt32 Version; public UInt32 Flags; [MarshalAs(UnmanagedType.LPWStr)] public string SimpleReasonString; } [StructLayout(LayoutKind.Sequential)] public struct PowerRequestContextDetailedInformation { public IntPtr LocalizedReasonmodulee; public UInt32 LocalizedReasonId; public UInt32 ReasonStringCount; [MarshalAs(UnmanagedType.LPWStr)] public string[] ReasonStrings; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct POWER_REQUEST_CONTEXT_DETAILED { public UInt32 Version; public UInt32 Flags; public PowerRequestContextDetailedInformation DetailedInformation; } #endregion /// <summary> /// Prevent screensaver, display dimming and power saving. This function wraps PInvokes on Win32 API. /// </summary> /// <param name="enableConstantDisplayAndPower">True to get a constant display and power - False to clear the settings</param> private void EnableConstantDisplayAndPower(bool enableConstantDisplayAndPower) { if (enableConstantDisplayAndPower) { // Set up the diagnostic string _PowerRequestContext.Version = POWER_REQUEST_CONTEXT_VERSION; _PowerRequestContext.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; _PowerRequestContext.SimpleReasonString = "Continuous measurement"; // your reason for changing the power settings; // Create the request, get a handle _PowerRequest = PowerCreateRequest(ref _PowerRequestContext); // Set the request PowerSetRequest(_PowerRequest, PowerRequestType.PowerRequestSystemRequired); PowerSetRequest(_PowerRequest, PowerRequestType.PowerRequestDisplayRequired); } else { // Clear the request PowerClearRequest(_PowerRequest, PowerRequestType.PowerRequestSystemRequired); PowerClearRequest(_PowerRequest, PowerRequestType.PowerRequestDisplayRequired); CloseHandle(_PowerRequest); } } 

使用PowerCreateRequest,PowerSetRequest和PowerClearRequest函数是首选的方法。 详细信息和示例代码(C / C#)位于http://msdn.microsoft.com/en-us/library/windows/hardware/gg463205.aspx

如果它睡着了,怎么样醒来呢?

http://www.enterprisenetworksandservers.com/monthly/art.php?1049

同样的技术适用于防止屏幕保护程序应该使用。 请参阅以编程方式防止启动Windows屏幕保护程序 。

请注意,某些安全设置可以覆盖此设置(强制计算机在一定时间后锁定)。