1

WMI ManagementEventWatcher 有一个奇怪的问题,我用它来跟踪进程的开始和结束。

public System.Management.ManagementEventWatcher process_spawn;
public System.Management.ManagementEventWatcher process_end;

public home_panel(){

        InitializeComponent();

    try{

        string scope = @"\\.\root\CIMV2"; // local machine

        process_spawn = new System.Management.ManagementEventWatcher(scope, "SELECT * FROM Win32_ProcessStartTrace");
        process_end = new System.Management.ManagementEventWatcher(scope, "SELECT * FROM Win32_ProcessStopTrace");

        process_spawn.EventArrived += new System.Management.EventArrivedEventHandler(process_spawned);

        process_end.EventArrived += new System.Management.EventArrivedEventHandler(process_ended);


        process_spawn.Start();
        process_end.Start();

    }catch(Exception exc){
        Console.WriteLine(exc);
    }
}

void process_spawned(object sender, System.Management.EventArrivedEventArgs e){
    process_spawn.Start();
    Console.WriteLine((string)e.NewEvent["ProcessName"]);
    //socket.Emit("process_started", (string)e.NewEvent["ProcessName"]);
}

void process_ended(object sender, System.Management.EventArrivedEventArgs e){
    process_spawn.Stop();
    Console.WriteLine((string)e.NewEvent["ProcessName"]);
    //socket.Emit("process_started", (string)e.NewEvent["ProcessName"]);
}

问题是,当我在 Visual Studio 2012 Express 的调试模式下运行程序时,一切正常,但是当我运行 .exe 文件时,它会抛出此异常:

System.OutOfMemoryException: Not enough memory to continue the execution of the program. 
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementEventWatcher.Initialize()
at System.Management.ManagementEventWatcher.Start()

我希望有人可以帮助我找出导致此异常的原因。

4

0 回答 0