等待进程直到启动c#

时间:2016-02-24 10:40:50

标签: c#

我需要在C#

中等待过程直到开始

赞:Autoit中的ProcessWait("notepad.exe")

https://www.autoitscript.com/autoit3/docs/functions/ProcessWait.htm

1 个答案:

答案 0 :(得分:2)

此处有更多信息:Process Information and Notifications using WMI

using System;
using System.Diagnostics;
using System.Management;

namespace Test
{
    public static class Program
    {
        static void Main(string[] args)
        {
            var queryString =
                "SELECT TargetInstance " +
                "FROM __InstanceCreationEvent WITHIN 1 " +
                "WHERE TargetInstance ISA 'Win32_Process' " +
                "AND TargetInstance.Name LIKE 'notepad.exe'";

            var scope = @"\\.\root\CIMV2";

            var watcher = new ManagementEventWatcher(scope, queryString);
            watcher.EventArrived += watcher_EventArrived;
            watcher.Start();
            Process.Start("notepad.exe");
            Console.ReadKey();
        }

        static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            Console.WriteLine("Notepad has been started");
        }
    }
}