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
La mayor parte de esto está cubierto por la clase SystemEvents. –