.NET WinForms - How to listen for events for LogOff, User Locked, Hibernate Started and System resumes?

I want to listen for events in my windows .NET applications for the following system events:

Logout Windows Lock Sleep Mode Started System Resumed

Is it possible?

thanks

+5
source share
2 answers
+4

, WMI .
, (, , VS2010 .Net3.5)

,

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

Win32,

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

, , , , , , MSDN.

, ,
Liron

+1

All Articles