C# 我如何知道 Windows 何时进入/退出睡眠或休眠模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/228288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I know when Windows is going into/out of sleep or Hibernate mode?
提问by Chris Thompson
Is it possible to subscribe to a Windows event that fires when Windows is going into or coming out of Sleep or Hibernate state?
是否可以订阅在 Windows 进入或退出睡眠或休眠状态时触发的 Windows 事件?
I need my application to be made aware when the computer is going to sleep to do some cleanup and avoid timing issues when it comes out of sleep.
我需要让我的应用程序知道计算机何时进入睡眠状态以进行一些清理并避免在它退出睡眠时出现计时问题。
采纳答案by Jeff Yates
Microsoft.Win32.SystemEvents.PowerModeChanged
event will give you this information. This event is available in all variants of the .NET framework released by Microsoft so far.
Microsoft.Win32.SystemEvents.PowerModeChanged
活动将为您提供此信息。迄今为止,该事件在 Microsoft 发布的所有 .NET 框架变体中都可用。
回答by aku
You can monitor Win32_PowerManagementEventWMI event
您可以监控Win32_PowerManagementEventWMI 事件
回答by Hugh Allen
In .NET, use the PowerModeChangedevent. In Win32, use the WM_POWERBROADCASTmessage.
在 .NET 中,使用PowerModeChanged事件。在 Win32 中,使用WM_POWERBROADCAST消息。
回答by grettke
Not sure how often you want to monitor this, but if you write a service in .NET you can override ServiceBase, set CanHandlePowerEvent to true, and then you'll be notified of power changes via the PowerBroadcastStatus enumeration.
不确定您希望多久监控一次,但如果您在 .NET 中编写服务,您可以覆盖 ServiceBase,将 CanHandlePowerEvent 设置为 true,然后您将通过 PowerBroadcastStatus 枚举通知电源更改。
回答by Richard Chambers
In a Visual Studio 2005 C++ MFC application you will need to add an ON_MESSAGE()
to your message map looking for the WM_POWERBROADCAST
message as in this example:
在 Visual Studio 2005 C++ MFC 应用程序中,您需要ON_MESSAGE()
在消息映射中添加一个以查找WM_POWERBROADCAST
消息,如下例所示:
BEGIN_MESSAGE_MAP(CFrameworkWndDoc, CWindowDocument)
//{{AFX_MSG_MAP(CFrameworkWndDoc)
ON_WM_CHAR()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_POWERBROADCAST, OnPowerMsgRcvd)
END_MESSAGE_MAP()
Then you will need to add the message handler function along with the class definition change to declare the member function for the message handler so that you can check the wParam
variable for the message type as in this skeleton:
然后,您需要添加消息处理程序函数以及类定义更改,以声明消息处理程序的成员函数,以便您可以检查wParam
消息类型的变量,如下面的骨架:
// Handle the WM_POWERBROADCAST message to process a message concerning power management
// such as going to Sleep or Waking Up.
LRESULT CFrameworkWndDoc::OnPowerMsgRcvd(WPARAM wParam, LPARAM lParam)
{
switch (wParam) {
case PBT_APMPOWERSTATUSCHANGE:
TRACE0("PBT_APMPOWERSTATUSCHANGE received\n");
break;
case PBT_APMRESUMEAUTOMATIC:
TRACE0("PBT_APMRESUMEAUTOMATIC received\n");
break;
case PBT_APMRESUMESUSPEND:
TRACE0("PBT_APMRESUMESUSPEND received\n");
break;
case PBT_APMSUSPEND:
TRACE0("PBT_APMSUSPEND received\n");
break;
}
return 0;
}
What I have seen is that a test using the above in an application running on Windows 7 that is started in the debugger and then I manually make my PC running the application to Sleep I will see the following message:
我所看到的是,在调试器中启动的 Windows 7 上运行的应用程序中使用上述测试,然后我手动使我的 PC 运行该应用程序进入睡眠状态,我将看到以下消息:
PBT_APMSUSPEND received
Then when the PC is restarted and I sign-in what I will see in the debugger output window are two messages one after the other:
然后,当 PC 重新启动并登录时,我将在调试器输出窗口中看到一条接一条的两条消息:
PBT_APMRESUMESUSPEND received
PBT_APMRESUMEAUTOMATIC received
Everything that I have found thus far indicates that you have no indication whether you are coming out of a Sleep state or a Hibernate state. I am still doing further research on what needs to be done when suspending or when resuming so far as file and device handles. I have seen indications that file handles to COM ports are no longer valid after resuming. I also am unsure about interfaces to other processes for instance database connections.
到目前为止,我发现的所有内容都表明您没有任何迹象表明您是从睡眠状态还是休眠状态中出来。我仍在进一步研究暂停或恢复时需要做什么,就文件和设备处理而言。我已经看到恢复后 COM 端口的文件句柄不再有效的迹象。我也不确定与其他进程的接口,例如数据库连接。
In addition to the standard Sleep and Hibernate power management states Microsoft has introduced the Connected Standby power state with Windows 8 and 8.1which has some application design ramifications depending on the type of application.
除了标准的睡眠和休眠电源管理状态之外,Microsoft 还在Windows 8 和 8.1 中引入了连接待机电源状态,根据应用程序的类型,它具有一些应用程序设计分支。
Desktop applications typically require no extra work to integrate with connected standby.
The Desktop Activity Moderator (DAM) is the Windows component that pauses all desktop applications and throttles the runtime of third-party system services during connected standby. The purpose of the DAM is to maintain basic software compatibility with existing applications and services, but mitigate their impact on battery life during sleep.
Windows prevents desktop applications from running during any part of connected standby after the DAM phase completes. Windows allows third-party system services to execute in a throttled mode after completing the DAM phase. In this mode, a third-party service can run for no more than one second of wall-clock time every 30 seconds.
桌面应用程序通常不需要额外的工作即可与联网待机集成。
桌面活动调节器 (DAM) 是 Windows 组件,可在连接待机期间暂停所有桌面应用程序并限制第三方系统服务的运行时间。DAM 的目的是保持与现有应用程序和服务的基本软件兼容性,但减轻它们在睡眠期间对电池寿命的影响。
在 DAM 阶段完成后,Windows 会阻止桌面应用程序在连接待机的任何部分运行。Windows 允许第三方系统服务在完成 DAM 阶段后以节流模式执行。在这种模式下,第三方服务每 30 秒运行的时间不超过挂钟时间的一秒。
The Art of Graceful Application Suspension by Lynn Merrillfrom Intel has some information about handling the various Windows message types associated with Power Management under Windows however it is date 2005 so not all material may pertain to Windows after Windows XP. There is at least one no longer used message in the message sequence described in this document as beginning with Windows Vista the PBT_APMQUERYSUSPEND
message which was used to request whether an application was able to suspend is no longer used by Windows. The SetThreadExecutionState()
function is now used to indicate that a thread can not be interrupted with a change to Sleep or Hibernate state. See the answers in stackoverflow Can't catch sleep suspend messages (winxp)for details on Power Management state message changes.
来自英特尔的 Lynn Merrill 的“优雅应用程序暂停艺术”提供了一些有关处理与 Windows 下的电源管理相关的各种 Windows 消息类型的信息,但它是 2005 年的,因此并非所有材料都适用于 Windows XP 之后的 Windows。在本文档中描述的消息序列中至少有一个不再使用的消息,从 Windows Vista 开始,PBT_APMQUERYSUSPEND
用于请求应用程序是否能够挂起的消息不再被 Windows 使用。该SetThreadExecutionState()
函数现在用于指示不能通过更改为睡眠或休眠状态来中断线程。有关电源管理状态消息更改的详细信息,请参阅 stackoverflow Can't catch sleep suspend messages (winxp) 中的答案。
回答by Fred Stephens
You can subscribe to NetworkChange.NetworkAvailabilityChanged and NetworkChange.NetworkAddressChanged.
您可以订阅 NetworkChange.NetworkAvailabilityChanged 和 NetworkChange.NetworkAddressChanged。
I generally start a two second timer so that I can resume network communications after being in sleep mode when it times out.
我通常会启动一个两秒钟的计时器,以便在超时后进入睡眠模式后可以恢复网络通信。