创建流程时是否有系统事件? [英] Is there a System event when processes are created?

查看:173
本文介绍了创建流程时是否有系统事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建新进程时是否有任何事件。我正在编写一个检查某些进程的c#应用程序,但是我不想写一个无限循环来连续遍历所有已知的进程。相反,我宁愿检查通过事件触发的所有当前进程创建或迭代的每个进程。任何建议?

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 through all known processes continuously. Instead, I rather check each process that is created or iterate through all current processes triggered by an event. Any suggestions?

        Process[] pArray;
        while (true)
        {
            pArray = Process.GetProcesses();

            foreach (Process p in pArray)
            {
                foreach (String pName in listOfProcesses)  //just a list of process names to search for
                {

                    if (pName.Equals(p.ProcessName, StringComparison.CurrentCultureIgnoreCase))
                    {
                       //do some stuff

                    }
                }
            }

            Thread.Sleep(refreshRate * 1000);
        }


推荐答案

WMI为您提供了一种方法聆听流程的创建(还有大约一百万的其他东西)。请参阅我在这里的答案

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();
    }
}

这篇关于创建流程时是否有系统事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆