在 Visual Basic.NET 中获取睡眠/休眠和恢复/唤醒事件 [英] Get Sleep/Hibernate and Resume/Wakeup events in Visual Basic.NET
问题描述
我有与一些外部服务器通信的 VB.NET 应用程序(通过 Intranet 维护登录会话),我想监听睡眠/休眠事件,这样当它发生时,我想在计算机运行之前注销现有的会话系统睡觉,而我的应用程序将继续在后台运行但不会做任何事情.
I have VB.NET app that communicates with some external server (maintains login sessions via Intranet), and I want to listen for Sleep/Hibernate events such that when it happens, I want to logout an existing session system before computer goes to sleep, while my app will remain running in the background but won't do anything.
反之亦然,当计算机从休眠状态恢复或从睡眠中醒来时,我想立即登录服务器.
And vice versa, when computer is resumed from Hibernate or Woke up from sleep, I want to immediately login to the server.
如何获取这些事件并执行我的代码?我相信这与我应该在 VB 中使用的 Win32 API 有关.
How can I grab those events and execute my code? I believe this relates to Win32 API which I'm supposed to use in VB.
谢谢.
推荐答案
如果您的应用程序正在使用窗口并且可以读取消息循环,您只需订阅 Microsoft.Win32.SystemEvents.PowerModeChanged 像:
If your application is using a window and can read the message loop you can simply subscribe to Microsoft.Win32.SystemEvents.PowerModeChanged like:
AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf SystemEvents_PowerModeChanged
Private Sub SystemEvents_PowerModeChanged(ByVal sender As Object, ByVal e As PowerModeChangedEventArgs)
Select Case e.Mode
Case PowerModes.Resume
Case PowerModes.StatusChange
Case PowerModes.Suspend
End Select
End Sub
此外,例如,如果您正在使用一项服务,则必须创建一个隐藏的表单,如这里(向下滚动到示例 2")
Else, if you are using a service for example, you have to create a hidden form like described here (scroll down to 'Example 2')
这篇关于在 Visual Basic.NET 中获取睡眠/休眠和恢复/唤醒事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!