Detect PC Resume + Detect if program is opening

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
Hi there,

I have two questions.

The first is: How do I detect if the PC is coming out of Standby?

Second is: How do I detect if a certain program is opening?

Thanks.

EDIT:

I think i have found the answer to my first questions in the snippets :S....should look properly.
 
VB.NET:
Private Sub frmDiv_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    RemoveHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf PowerModeChanged
End Sub

Private Sub frmDiv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf PowerModeChanged
End Sub

Sub PowerModeChanged(ByVal sender As Object, ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
    Select Case e.Mode
        Case Microsoft.Win32.PowerModes.Suspend

        Case Microsoft.Win32.PowerModes.Resume

    End Select
End Sub
To check for process starts you could use WMI and the ProcessStartTrace event, WMI Code Creator should get you some code for this, your VB.Net application must be in Release mode to catch it.
 
I am trying this, but it doesn't seem to trigger the event:


VB.NET:
    Dim WithEvents systemEvent As SystemEvents

    Private Sub On_Standby_Resume(ByVal sender As Object, ByVal e As Microsoft.Win32.PowerModeChangedEventArgs) Handles systemEvent.PowerModeChanged

        If e.Mode = PowerModes.Resume Then

            MessageBox.Show("Resuming")

        End If

    End Sub
 
The code I posted does, it's probably because it is a Shared event that doesn't operate through an instance. Your handles reference a nullref variable "systemEvent". You can still write that withevent line to help you generate the handler methods, but remove that and the handles clause afterwards and use the AddHandler/RemoveHandler statements to have it operate properly. Did you also read the note in documentation that you have to remove the handler for this event when disposing to prevent memory leaks?
 
So you are right. (No Surprise! :p)

...But its a bit strange, because the code I produced came straight out of the Microsoft Default Snippets IIRC.
 
Back
Top