2010-02-11 5 views

Respuesta

4

Es necesario buscar WMI (Windows media instrumentación). Debería crear observadores de eventos para los eventos mencionados anteriormente.

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

Enlaces de interés:

Get Log off event from system

Cómo crear un Monitor de eventos WMI para un evento de cierre de sesión de usuario?

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

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

1

Como se sugirió anteriormente, se puede utilizar el WMI para eventos trampa.
estoy añadiendo algunos ejemplos de código que escribí hace unos años (espero que seguirá funcionando desde que fue escrito en VS2010 Con .Net3.5)

Aquí está una clase que reúne todos los eventos

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 

Ahora, para el oyente, que tenía un servicio Win32 que corría en el fondo y hacía el trabajo de escucha

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

me siento que no tengo ninguna referencia para los sitios que he tomado de los ejemplos, Supongo que fue probablemente de la MS DN enlaces arriba.

esperan que los pueda ayudar,
Liron

Cuestiones relacionadas