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
4

2 回答 2

1

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

 Dim processList As String
 For Each p As Process In Process.GetProcesses()
    processList = processList & " " & p.SessionId & vbNewLine
 Next()
 MsgBox(processList)
于 2013-04-12T09:33:35.693 回答
1

管理事件观察者可以做你正在寻找的东西。看看: ManagementEventWatcher.Stop 方法

于 2013-12-05T05:37:02.560 回答