2013-04-12 44 views
0

我該怎麼做?我不想使用一個定時器+一個進程數組(因爲這是我總是做的新手),我正在尋找更好的東西和改進。監控所有進程的執行情況

我使用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 

回答

1

管理事件觀察可以做究竟你正在尋找。 看一看: ManagementEventWatcher.Stop Method

+1

請嘗試閱讀此http://stackoverflow.com/help/deleted-answers,以獲得更多的理解如何**不**回答。即:「不能從根本上回答問題的答案」:**僅僅是一個鏈接到外部網站** –

1

這將獲取名稱和每個正在運行的進程的PID,只需修改這個代碼,以獲得任何額外的信息......

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

三江源但我知道的方法,它是隻對你說的話,你的榜樣得到所有正在運行的進程,然後監控所有新開通流程的執行首先我需要得到一個如你的例子中的進程數組,然後經過「X」毫秒的時間我需要獲得另一個進程數組並將其與舊數組進行比較以搜索新條目。我知道如何做到這一點,但我真的想逃避這種方法,這就是爲什麼我要問如何使用更多和簡化爲ManagementEventWatcher,但真的非常感謝您的答案 – ElektroStudios

+0

只需使用計時器,然後調用子與Tick事件比較兩個列表... – Sam

+0

@山姆我知道,但我喜歡我說我preffer使用另一種方式,謝謝 – ElektroStudios