WMI cung cấp cách theo dõi quá trình bắt đầu và kết thúc bằng các lớp Win32_ProcessTrace. Tốt nhất được hiển thị với một ví dụ. Bắt đầu một ứng dụng Console mới, Project + Add Reference, chọn System.Management. Dán mã này:
using System;
using System.Management;
class Process {
public static void Main() {
ManagementEventWatcher startWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
startWatch.EventArrived += new EventArrivedEventHandler(startWatch_EventArrived);
startWatch.Start();
ManagementEventWatcher stopWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));
stopWatch.EventArrived += new EventArrivedEventHandler(stopWatch_EventArrived);
stopWatch.Start();
Console.WriteLine("Press any key to exit");
while (!Console.KeyAvailable) System.Threading.Thread.Sleep(50);
startWatch.Stop();
stopWatch.Stop();
}
static void stopWatch_EventArrived(object sender, EventArrivedEventArgs e) {
Console.WriteLine("Process stopped: {0}", e.NewEvent.Properties["ProcessName"].Value);
}
static void startWatch_EventArrived(object sender, EventArrivedEventArgs e) {
Console.WriteLine("Process started: {0}", e.NewEvent.Properties["ProcessName"].Value);
}
}
Edit the manifest để chương trình này chạy cao. Sau đó, chỉ cần bắt đầu một số chương trình để xem nó tại nơi làm việc. Cẩn thận rằng nó không phải là đặc biệt nhanh chóng.
Nguồn
2009-12-31 19:44:22
Cảm ơn bạn. Chỉ cần thử nó và nó hoạt động hoàn hảo. –
Nó hoạt động hoàn hảo, nhưng sự chậm trễ là gây phiền nhiễu, có anyway để có được nó nhanh hơn? –
Tôi vừa thử nó và có độ trễ ít hơn 1 giây. Tôi nghĩ rằng điều này là ok. – webber2k6