Windows 服务的 ResumeAutomatic 和 ResumeSuspend 模式之间的区别

Difference between ResumeAutomatic & ResumeSuspend modes of Windows Service

根据MSDN documentation

ResumeAutomatic : The computer has woken up automatically to handle an event.

Note : If the system detects any user activity after broadcasting ResumeAutomatic, it will broadcast a ResumeSuspend event to let applications know they can resume full interaction with the user.

ResumeSuspend : The system has resumed operation after being suspended.

这是否意味着'ResumeAutomatic'在计算机从睡眠中唤醒时调用,而'ResumeSuspend'在用户输入凭据后登录时调用?

我正在使用 tcp 套接字与服务器通信。因此,为了在系统从休眠状态恢复时重新连接到服务,我有以下代码

    protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
    {
        Logger.Log("Power status is : " + powerStatus);
        if (powerStatus == PowerBroadcastStatus.ResumeAutomatic)
        {
            ConnectionWatchdog.ReConnect();
        }
        return base.OnPowerEvent(powerStatus);
    }

但我观察到枚举值是随机的。以下是 3 个不同唤醒时间的 3 个不同轨迹。

20150525#094449 :: 电源状态为:暂停

20150525#094716 :: 电源状态为:ResumeSuspend


20150525#103431 :: 电源状态为:暂停

20150525#103525 :: 电源状态为:ResumeSuspend

20150525#103525 :: 电源状态为:ResumeAutomatic


20150525#103558 :: 电源状态为:暂停

20150525#103835 :: 电源状态为:ResumeAutomatic

它应该如何工作

(这在实践中并非如此 - 见下文。)

自动恢复

计算机在休眠后恢复时始终发送此消息。

恢复挂起

计算机在休眠后恢复,Windows 认为有用户在场 - 即有人坐在机器前。当 a) 唤醒是由人为交互引起的(有人按下电源按钮、按键、移动鼠标​​等)时发送此消息;或 b) 机器由于唤醒定时器而自动唤醒后第一次出现人机交互。

为了直接回答您的问题,ResumeSuspend 会在用户第一次与计算机交互时发送。这可能是输入密码来解锁它,但它不一定是。如果用户只是摆动鼠标,ResumeSuspend 仍将被发送。

总结一下:

  1. ResumeAutomatic 总是在计算机从睡眠状态恢复时发送。
  2. ResumeSuspend 发送 以及 ResumeAutomatic 当计算机从睡眠和 Windows 认为用户存在。

实际工作原理

  1. ResumeAutomatic 偶尔根本不发送。这是一个长期存在的错误,大概在 Windows 本身。幸运的是,我从未见过 both ResumeAutomaticResumeSuspend 未发送的计算机唤醒。 如果您需要知道系统已恢复,但又不关心用户是否在,则需要监听 ResumeAutomaticResumeSuspend 并将它们视为同一事物。
  2. ResumeSuspend 极其不可靠。我从未见过它 not 在它应该发送的时候发送,但它经常在它 不是 应该发送的时候发送 - 实际上有根本没有用户。这是否是由于 Windows、第三方驱动程序、固件或硬件中的一个或多个错误,我不知道。
  3. 当发送 ResumeAutomatic 而没有相应的 ResumeSuspend 时,系统空闲超时很短(Windows 中默认为 2 分钟10) 和连接的显示器保持省电模式。当发送相应的 ResumeSuspend 时,系统空闲超时是正常的(Windows 10 中默认为 30 分钟)并且连接的显示器被唤醒。这样做是为了让计算机在自动唤醒进行维护等时尽快回到睡眠状态。如果微软能让它可靠地工作,那就太好了。

我不幸不得不深入 Windows 对电源管理、配置文件等的支持。Vista 时代的东西令人沮丧,因为它基于质量和周到的设计,但实现和文档都不完全,并且从未修复。还有许多其他琐碎的问题我没有在这里讨论。都有些可惜了。