监控所有流程的执行情况

时间:2013-04-12 08:21:30

标签: vb.net visual-studio process monitoring monitor

我怎么做?

我不想使用计时器+一系列进程(因为这是我总是这样做的新手方式)我正在寻找更好和改进的东西。

我尝试过使用ManagementEventWatcher但是这个代码示例在Windows 7中不起作用(我没有收到任何错误,但没有显示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

2 个答案:

答案 0 :(得分:1)

这将获取每个正在运行的进程的名称和PID,只需修改此代码即可获得任何其他信息......

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

答案 1 :(得分:1)

管理事件观察者可以完成您正在寻找的事情。 看一看: ManagementEventWatcher.Stop Method