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

标签 c# .net windows-services

根据 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.

这是否意味着当计算机从 sleep 中唤醒时调用“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

最佳答案

它应该如何工作

(这并不是它在实践中的全部运作方式 - 见下文。)

自动恢复

当计算机在 sleep 后恢复时总是发送此消息。

恢复挂起

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

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

总结一下:

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

它是如何运作的

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

我不幸不得不深入 Windows 对电源管理、配置文件等的支持。Vista 时代的东西令人沮丧,因为它基于质量和深思熟虑的设计,但两者实现和文档不完整,并且从未修复过。还有许多其他琐碎的问题我没有在这里讨论。有点遗憾。

关于c# - Windows 服务的 ResumeAutomatic 和 ResumeSuspend 模式之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30433432/

相关文章:

c# - 从 .NET Standard 库获取 Windows 系统目录

c# - 如何重新创建 "Index was outside the bounds of the array"添加项目到字典?

windows - 用于重新启动 Windows 服务的桌面快捷方式

c# - 是否需要 relaycommand 的异步版本才能正确运行异步方法

c# - 将类列表序列化为 XML

java - .net 到 Java - 序列化的 XML 可以工作吗?

.net - 在 using 语句中调用 Task.Run 可以吗?

c# - 如何解决 "Cross-thread operation not valid"?

c# - 将应用程序作为 Windows 服务运行时程序集文件加载错误

c# - BadImageFormatException x64 问题