Is there a System event when processes are created?

前端 未结 1 425
旧巷少年郎
旧巷少年郎 2021-02-05 14:36

Is there any event when a new process is created. I\'m writing a c# application that checks for certain processes, but I don\'t want to write an infinite loop to iterate throug

1条回答
  •  我在风中等你
    2021-02-05 15:17

    WMI gives you a means to listen for process creation (and about a million other things). See my answer here.

     void WaitForProcess()
    {
        ManagementEventWatcher startWatch = new ManagementEventWatcher(
          new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
        startWatch.EventArrived
                            += new EventArrivedEventHandler(startWatch_EventArrived);
        startWatch.Start();
    }
    
    static void startWatch_EventArrived(object sender, EventArrivedEventArgs e)
    {
        Console.WriteLine("Process started: {0}"
                          , e.NewEvent.Properties["ProcessName"].Value);
        if (this is the process I'm interested in)
        {
                 startWatch.Stop();
        }
    }
    

    0 讨论(0)
提交回复
热议问题