I'm using a device driver which I need to close when the system goes to sleep. I thought I could add a handler for the SystemEvents.PowerModeChanged event to ApplicationEvents.vb as follows:
Public Sub SystemEvents_PowerModeChanged(sender As Object, e As PowerModeChangedEventArgs) _
Handles SystemEvents.PowerModeChanged
If e.Mode = PowerModes.Suspend Then
logger.Trace("The computer is suspending. Closing device.")
'Close device
ElseIf e.Mode = PowerModes.Resume Then
logger.Trace("The computer is resuming operation from suspend mode. Opening device.")
' Open device
End If
End Sub
to handle the event using the Handles
keyword. However, there's a compilation error on the Handles clause:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
WithEvents seems to always be defined for the other events I encounter in .NET, so why is this one different? I would prefer to use the Handles clause, because this automates the addition and removal of the handler. It also keeps the functionality self-contained instead of spreading it between the initializer, destructor, and the function itself. Is it possible to use the Handles declaration on this event?
A clue to this problem is that the following code does work:
AddHandler SystemEvents.PowerModeChanged, AddressOf SystemEvents_PowerModeChanged