簡體   English   中英

監控所有流程的執行情況

[英]Monitoring the execution of all process

我怎么能這樣做?

我不想使用計時器+一系列進程(因為這是我總是這樣做的新方式)我正在尋找更好的和改進的東西。

我嘗試過使用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

這將獲取每個正在運行的進程的名稱和PID,只需修改此代碼即可獲得任何其他信息......

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

管理事件觀察者可以完成您正在尋找的事情。 看看: ManagementEventWatcher.Stop方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM