当我的程序正在运行时,我必须阻止windows进入睡眠状态。
我不仅想阻止休眠定时器,我还想取消休眠事件,如果我按下休眠按钮或以任何其他方式主动告诉计算机休眠。因此,SetThreadExecutionState是不够的。
或者...我实际上不必完全阻止睡眠,只需延迟5-10秒就可以让我的程序完成一项任务。
(我知道这是不好的程序行为,但它仅供个人使用。)
发布于 2009-03-10 08:06:42
我在通过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);发布于 2014-01-08 20:48:49
在考虑了vim's answer之后
“使用PowerCreateRequest、PowerSetRequest和PowerClearRequest函数是首选方法。”
由于msdn上的链接AvailabilityRequests.docx很累人(读起来太多了),我在网上搜索了一个基于PowerCreateRequest的c#的具体示例,发现http://go4answers.webhost4life.com/Example/problem-monitor-wakeup-service-windows7-12092.aspx EDIT 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 LocalizedReasonModule;
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);
}
}发布于 2013-04-18 23:03:36
使用PowerCreateRequest、PowerSetRequest和PowerClearRequest函数是首选方法。详细信息和示例代码(C/C#)在http://msdn.microsoft.com/en-us/library/windows/hardware/gg463205.aspx中
https://stackoverflow.com/questions/629240
复制相似问题