.NET WinForms -如何侦听系统下线、用户锁定、休眠和系统恢复的事件?

时间:2021-07-22 03:27:00

I want to listen to events in my windows forms .NET application for the following system wide events :

我想在我的windows窗体。net应用程序中监听以下系统范围内的事件:

Log Off Lock Windows Hibernate Started Sleep Started System Resumed

休眠启动睡眠启动系统恢复

Are these possible?

这些是可能的吗?

Thanks

谢谢

2 个解决方案

#1


4  

You need to look WMI (Windows media instrumentation). You would need to create event watchers for the above mentioned events.

您需要查看WMI (Windows media instrumentation)。您需要为上述事件创建事件观察者。

http://msdn.microsoft.com/en-us/library/ms257340%28VS.80%29.aspx

http://msdn.microsoft.com/en-us/library/ms257340%28VS.80%29.aspx

Useful links:

有用的链接:

Get Log off event from system

从系统获取注销事件

How to create a WMI Event Watcher for a user logoff event?

如何为用户注销事件创建WMI事件监视程序?

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/0c1bded8-0cce-4260-bd28-4b4ffce0d27d

http://social.msdn.microsoft.com/forums/en us/netfxbcl/thread/0c1bded8 - 0 - cce - 4260 bd28 b4ffce0d27d——4

http://www.aspfree.com/c/a/VB.NET/WMI-Programming-with-Visual-BasicNET-Trapping-System-Events/1/

http://www.aspfree.com/c/a/VB.NET/WMI-Programming-with-Visual-BasicNET-Trapping-System-Events/1/

#2


1  

As suggested above, you can use the WMI to trap events.
I'm adding some code sample I wrote few years back (hope it will still work since it was written on VS2010 With .Net3.5)

如上所述,您可以使用WMI来捕获事件。我正在添加几年前编写的一些代码示例(希望它仍然可以工作,因为它是在VS2010和.Net3.5编写的)

Here's a class that gathers all the events

这是一个收集所有事件的类

Imports Microsoft.Win32
Imports System.Windows.Forms

Public Class PowerMessageFilter
    Implements IMessageFilter
    Const WM_POWERBROADCAST As Integer = &H218
    Const PBT_APMSUSPEND As Integer = &H4
    Const PBT_APMSTANDBY As Integer = &H5
    Const PBT_APMRESUMESUSPEND As Integer = &H7
    Const PBT_APMRESUMESTANDBY As Integer = &H8

   Protected Sub reportpowerchange(ByVal reason As Integer)
       Dim report As String = String.Empty
       Select Case (reason)
           Case PBT_APMSUSPEND
               report = "system is suspending operation "
               suspend_service()
               Exit Select
           Case PBT_APMSTANDBY
               report = "system is standing by "
               suspend_service()
               Exit Select
           Case PBT_APMRESUMESUSPEND
               report = "operation resuming after suspension."
               suspend_service()
               Exit Select
           Case PBT_APMRESUMESTANDBY
               report = "operation resuming after stand by."
               suspend_service()
           Exit Select
       End Select
   End Sub

   Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
       If WM_POWERBROADCAST = m.Msg Then
           Console.Out.WriteLine("Power Broadcast recieved.")
           Dim reason As Integer = m.WParam.ToInt32()
           reportpowerchange(reason)
       End If
       Return False
   End Function

   Private Sub suspend_service()
      ' Your suspend code
   End Sub
End Class

Now, for the listener, I had a Win32 service that ran in the background and did the listening work

现在,对于侦听器,我有一个在后台运行的Win32服务,并执行侦听工作

Dim Filter As New PowerMessageFilter 'The Standby/Hibernation Filter catch;
Application.AddMessageFilter(Filter)

I sorry that I don't have any references for the sites I've taken the examples from, I guess it was probably from the MSDN links above.

很抱歉,我没有任何参考资料,我从这些例子,我想它可能来自上面的MSDN链接。

Hope it can help,
Liron

希望这能有所帮助,Liron

#1


4  

You need to look WMI (Windows media instrumentation). You would need to create event watchers for the above mentioned events.

您需要查看WMI (Windows media instrumentation)。您需要为上述事件创建事件观察者。

http://msdn.microsoft.com/en-us/library/ms257340%28VS.80%29.aspx

http://msdn.microsoft.com/en-us/library/ms257340%28VS.80%29.aspx

Useful links:

有用的链接:

Get Log off event from system

从系统获取注销事件

How to create a WMI Event Watcher for a user logoff event?

如何为用户注销事件创建WMI事件监视程序?

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/0c1bded8-0cce-4260-bd28-4b4ffce0d27d

http://social.msdn.microsoft.com/forums/en us/netfxbcl/thread/0c1bded8 - 0 - cce - 4260 bd28 b4ffce0d27d——4

http://www.aspfree.com/c/a/VB.NET/WMI-Programming-with-Visual-BasicNET-Trapping-System-Events/1/

http://www.aspfree.com/c/a/VB.NET/WMI-Programming-with-Visual-BasicNET-Trapping-System-Events/1/

#2


1  

As suggested above, you can use the WMI to trap events.
I'm adding some code sample I wrote few years back (hope it will still work since it was written on VS2010 With .Net3.5)

如上所述,您可以使用WMI来捕获事件。我正在添加几年前编写的一些代码示例(希望它仍然可以工作,因为它是在VS2010和.Net3.5编写的)

Here's a class that gathers all the events

这是一个收集所有事件的类

Imports Microsoft.Win32
Imports System.Windows.Forms

Public Class PowerMessageFilter
    Implements IMessageFilter
    Const WM_POWERBROADCAST As Integer = &H218
    Const PBT_APMSUSPEND As Integer = &H4
    Const PBT_APMSTANDBY As Integer = &H5
    Const PBT_APMRESUMESUSPEND As Integer = &H7
    Const PBT_APMRESUMESTANDBY As Integer = &H8

   Protected Sub reportpowerchange(ByVal reason As Integer)
       Dim report As String = String.Empty
       Select Case (reason)
           Case PBT_APMSUSPEND
               report = "system is suspending operation "
               suspend_service()
               Exit Select
           Case PBT_APMSTANDBY
               report = "system is standing by "
               suspend_service()
               Exit Select
           Case PBT_APMRESUMESUSPEND
               report = "operation resuming after suspension."
               suspend_service()
               Exit Select
           Case PBT_APMRESUMESTANDBY
               report = "operation resuming after stand by."
               suspend_service()
           Exit Select
       End Select
   End Sub

   Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
       If WM_POWERBROADCAST = m.Msg Then
           Console.Out.WriteLine("Power Broadcast recieved.")
           Dim reason As Integer = m.WParam.ToInt32()
           reportpowerchange(reason)
       End If
       Return False
   End Function

   Private Sub suspend_service()
      ' Your suspend code
   End Sub
End Class

Now, for the listener, I had a Win32 service that ran in the background and did the listening work

现在,对于侦听器,我有一个在后台运行的Win32服务,并执行侦听工作

Dim Filter As New PowerMessageFilter 'The Standby/Hibernation Filter catch;
Application.AddMessageFilter(Filter)

I sorry that I don't have any references for the sites I've taken the examples from, I guess it was probably from the MSDN links above.

很抱歉,我没有任何参考资料,我从这些例子,我想它可能来自上面的MSDN链接。

Hope it can help,
Liron

希望这能有所帮助,Liron