简体   繁体   中英

Monitoring the execution of all process

How I can do it?

I don't want to use a timer + an array of processes (because that's the newbie way that I allways did) I'm looking for something better and improved.

I've tried using ManagementEventWatcher but this code example is not working in Windows 7 (I don't get any error, but is not showing the MsgBoxes):

Public Class Form1

Dim WithEvents startWatch As New System.Management.ManagementEventWatcher(New System.Management.WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"))
Dim WithEvents stopWatch As New System.Management.ManagementEventWatcher(New System.Management.WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"))

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler startWatch.EventArrived, AddressOf startWatch_EventArrived
    AddHandler stopWatch.EventArrived, AddressOf stopWatch_EventArrived
    startWatch.Start()
    stopWatch.Start()
End Sub

Public Sub startWatch_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs)
    MsgBox("Process started: " & e.NewEvent.Properties("ProcessName").Value)
End Sub

Private Sub stopWatch_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs)
    MsgBox("Process stopped: " & e.NewEvent.Properties("ProcessName").Value)
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    startWatch.Stop()
    stopWatch.Stop()
End Sub

End Class

This will get the name and PID of each running process, simply modify this code to get any additional information...

 Dim processList As String
 For Each p As Process In Process.GetProcesses()
    processList = processList & " " & p.SessionId & vbNewLine
 Next()
 MsgBox(processList)

Management Event Watcher can do what exactly you are looking. Have a look: ManagementEventWatcher.Stop Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM