简体   繁体   English

如何检测Windows服务中从睡眠模式唤醒?

[英]How to detect wake up from sleep mode in windows service?

Good day! 美好的一天! I write a windows service, which detects USB Flash drive connection and do something with connected drive. 我编写了一个Windows服务,该服务可检测USB闪存驱动器的连接,并对连接的驱动器执行某些操作。 And now after tests i need to reprocess devices after wake up from sleep mode. 现在经过测试后,我需要从睡眠模式唤醒后重新处理设备。 I solve this problem when service work as windows program. 当服务作为Windows程序工作时,我解决了这个问题。 To solve i additionally process DBT_DEVNODES_CHANGED , but service don't receive this message, it receives only DBT_DEVICE_ARRIVAL and DBT_DEVICEREMOVECOMPLETE . 为了解决这个问题,我还要处理DBT_DEVNODES_CHANGED ,但是服务没有收到此消息,它仅接收DBT_DEVICE_ARRIVALDBT_DEVICEREMOVECOMPLETE I also tried to check all messages handled by service except SERVICE_CONTROL_DEVICEEVENT and SERVICE_CONTROL_STOP when system is going to sleep and i don't get any message. 当系统进入睡眠状态并且我没有收到任何消息时,我还尝试检查由服务处理的所有消息,但SERVICE_CONTROL_DEVICEEVENTSERVICE_CONTROL_STOP除外。

How can I determine when the system is waking up? 如何确定系统何时醒来?

A normal application receives a WM_POWERBROADCAST:PBT_APMRESUMEAUTOMATIC window message, but a service will not get this message if it does not have a window. 普通应用程序会收到WM_POWERBROADCAST:PBT_APMRESUMEAUTOMATIC窗口消息,但是如果服务没有窗口,服务将不会收到此消息。 However, a service can receive SERVICE_CONTROL_POWEREVENT in its HandlerEx callback function, with the same parameters as WM_POWERBROADCAST 但是,服务可以在其HandlerEx回调函数中接收SERVICE_CONTROL_POWEREVENT ,其参数与WM_POWERBROADCAST相同

Notifies a service of system power events. 通知服务系统电源事件。 The dwEventType parameter contains additional information. dwEventType参数包含其他信息。 If dwEventType is PBT_POWERSETTINGCHANGE, the lpEventData parameter also contains additional information. 如果dwEventType为PBT_POWERSETTINGCHANGE,则lpEventData参数还包含其他信息。

On Windows 8 and later, you can also use the PowerRegisterSuspendResumeNotification() function: 在Windows 8和更高版本上,您还可以使用PowerRegisterSuspendResumeNotification()函数:

Registers to receive notification when the system is suspended or resumed. 注册以在系统挂起或恢复时接收通知。

The service equivalent of WM_DEVICECHANGE is SERVICE_CONTROL_DEVICEEVENT : WM_DEVICECHANGE的服务等效项是SERVICE_CONTROL_DEVICEEVENT

Notifies a service of device events. 通知服务设备事件。 (The service must have registered to receive these notifications using the RegisterDeviceNotification function.) The dwEventType and lpEventData parameters contain additional information. (该服务必须已注册才能使用RegisterDeviceNotification函数接收这些通知。)dwEventType和lpEventData参数包含其他信息。

The best way to detect that system is waking up is process power messages. 检测系统正在唤醒的最佳方法是过程电源消息。 Windows must process WM_POWERBROADCAST Services must add SERVICE_ACCEPT_POWEREVENT to last call to SetServiceStatus and process SERVICE_CONTROL_POWEREVENT Windows必须处理WM_POWERBROADCAST服务必须将SERVICE_ACCEPT_POWEREVENT添加到对SetServiceStatus最后一次调用并处理SERVICE_CONTROL_POWEREVENT

My error was very stupid. 我的错误非常愚蠢。 I forget to add SERVICE_ACCEPT_POWEREVENT . 我忘记添加SERVICE_ACCEPT_POWEREVENT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM