我该怎么做?
我不想使用计时器+一系列流程(因为这是我一直以来的新手方式)我正在寻找更好和改进的东西。
我尝试使用 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