当我的程序运行时,我必须阻止 Windows 进入睡眠状态。
而且我不仅想阻止睡眠定时器,如果我按下睡眠按钮或以任何其他方式主动告诉计算机睡眠,我还想取消睡眠事件。因此 SetThreadExecutionState 是不够的。
或者......我实际上不必完全阻止睡眠,只需将其延迟 5-10 秒以让我的程序完成任务。
(我知道这是不好的程序行为,但仅供个人使用。)
在考虑了vim的回答之后
“使用 PowerCreateRequest、PowerSetRequest 和 PowerClearRequest 函数是首选方法。”
使用msdn上链接的 AvailabilityRequests.docx很难进入它(阅读太多),我在网上搜索了一个基于 PowerCreateRequest的c#中的具体示例,并找到了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 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);
}
}
我在通过 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);
使用 PowerCreateRequest、PowerSetRequest 和 PowerClearRequest 函数是首选方法。详细信息和示例代码(C/C#)在http://msdn.microsoft.com/en-us/library/windows/hardware/gg463205.aspx
设置 wsc = CreateObject("WScript.Shell")
执行 WScript.Sleep (60*1000) wsc.SendKeys ("{SCROLLLOCK 2}") 循环
- 将上述代码放入记事本并将文件另存为 .vbs 并双击该文件
相同的技术适用于防止应使用屏幕保护程序。请参阅以编程方式阻止 Windows 屏幕保护程序启动。
请注意,某些安全设置可以覆盖此设置(强制计算机在特定时间后锁定为一)。
如果它进入睡眠状态,如何唤醒它?
http://www.enterprisenetworksandservers.com/monthly/art.php?1049
下面是我使用vim 建议SetThreadExecutionState
的现代电源可用性请求 API(取代)的尝试。
我正在使用我遇到的一个不错的 P/Invoke NuGet,Vanara.PInvoke.Kernel32:
using Vanara.PInvoke;
using static Vanara.PInvoke.Kernel32;
// create request object
using var request = PowerCreateRequest(new REASON_CONTEXT("App FOO is working"));
if (request.IsInvalid)
{
throw new InvalidOperationException(
$"Could not create power availability request: {Win32Error.GetLastError()}");
}
// send request
if (!PowerSetRequest(request, POWER_REQUEST_TYPE.PowerRequestSystemRequired))
{
throw new InvalidOperationException(
$"Could not send power availability request: {Win32Error.GetLastError()}");
}
// do stuff that required the machine to be up
Console.WriteLine("Doing stuff...");
await Task.Delay(5000);
// clear request
if (!PowerClearRequest(request, POWER_REQUEST_TYPE.PowerRequestSystemRequired))
{
Console.WriteLine(
"WARNING: Could not clear power availability request: {0}",
Win32Error.GetLastError());
}
powercfg /requests
您可以通过从管理终端发出来查看您的请求。
如果您需要一个在您的应用程序运行之前应该可以工作的显示器,请尝试设置“ES_DISPLAY_REQUIRED”而不是离开模式:
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
完成应用程序后,请确保清除所有其他标志。
SetThreadExecutionState(ES_CONTINUOUS);