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

前端 未结 12 2136
迷失自我
迷失自我 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 17:50

    Do you mean wait until it's done? Then use Process.WaitForExit:

    var process = new Process {
        StartInfo = new ProcessStartInfo {
            FileName = "popup.exe"
        }
    };
    process.Start();
    process.WaitForExit();
    

    Alternatively, if it's an application with a UI that you are waiting to enter into a message loop, you can say:

    process.Start();
    process.WaitForInputIdle();
    

    Lastly, if neither of these apply, just Thread.Sleep for some reasonable amount of time:

    process.Start();
    Thread.Sleep(1000); // sleep for one second
    

提交回复
热议问题