I have an WinForms application that keeps track when system enters suspended state (sleep) and when it resumes. App uses the SystemEvents
class for this purpose. It works fine on my machine. However it seems that for some users the event with PowerModes.Resume
is not always raised. App recieves multiple PowerModes.Suspend
without any PowerModes.Resume
in between which is strange.
My primary question is how that could possibly happen and how to avoid it and make resume detection reliable?
The code is pretty straightforward and is basically following (very shortened):
Imports Microsoft.Win32
Friend Class TehClass
Implements IDisposable
Private Sub New()
AddHandler SystemEvents.PowerModeChanged, AddressOf Me.System_PowerModeChanged
End Sub
Private Sub System_PowerModeChanged(sender As Object, args As PowerModeChangedEventArgs)
Log.Info("Power mode changed: {0}.", args.Mode)
End Sub
#Region "IDisposable Support"
' just dropping handler there
#End Region
End Class
There is an existing form within app all the time. However it could be minimized or hidden in the notification area. Also there is kept reference to the actual instance of the TehClass
.
This is the sample log:
[2015-09-15 22:38:38,501] Power mode changed: Suspend.
[2015-09-16 07:10:31,106] Power mode changed: Resume.
[2015-09-16 08:54:21,112] Power mode changed: Suspend.
[2015-09-16 09:14:36,252] Power mode changed: Suspend.
[2015-09-16 09:35:21,077] Power mode changed: Suspend.
[2015-09-16 09:55:36,085] Power mode changed: Suspend.
[2015-09-16 10:15:50.122] User reported this log therefore the PC had to be in Working (Resumed) state.
What I notice from the log is that there actually was Resume event raised for the first time. Another thing is that suspend got invoked in roughly 20 minutes intervals. Could it be some kind of "computer just partially wakes-up without actually turning monitor on or so and lets apps to process whatever they need to /network notifications, etc./ and than is put into sleep again" stuff?