C# - Making a Process.Start wait until the process has start-up

前端 未结 12 2114
迷失自我
迷失自我 2020-11-27 17:12

I need to make sure that a process is running before moving on with a method.

The statement is:

Process.Start(\"popup.exe\");

Can y

12条回答
  •  春和景丽
    2020-11-27 18:03

    First of all: I know this is rather old but there still is not an accepted answer, so perhaps my approach will help someone else. :)

    What I did to solve this is:

    process.Start();
    
    while (true)
    {
        try
        {
            var time = process.StartTime;
            break;
        }
        catch (Exception) {}
    }
    

    The association var time = process.StartTime will throw an exception as long as process did not start. So once it passes, it is safe to assume process is running and to work with it further. I am using this to wait for java process to start up, since it takes some time. This way it should be independent on what machine the application is running rather than using Thread.Sleep().

    I understand this is not very clean solution, but the only one that should be performance independent I could think of.

提交回复
热议问题