Wait until a process ends

2019-01-01 00:38发布

I've an application which does

Process.Start()

to start another application 'ABC'. I want to wait till that application ends (process dies) and continue my execution. How can I do it?

There may be multiple instances of the application 'ABC' running at the same time.

标签: c# .net process
8条回答
不再属于我。
2楼-- · 2019-01-01 01:07

I think you just want this:

var process = Process.Start(...);
process.WaitForExit();

See the MSDN page for the method. It also has an overload where you can specify the timeout, so you're not potentially waiting forever.

查看更多
余欢
3楼-- · 2019-01-01 01:10

Try this:

string command = "...";
var process = Process.Start(command);
process.WaitForExit();
查看更多
千与千寻千般痛.
4楼-- · 2019-01-01 01:11

Process.WaitForExit should be just what you're looking for I think.

查看更多
步步皆殇っ
5楼-- · 2019-01-01 01:12

You could use wait for exit or you can catch the HasExited property and update your UI to keep the user "informed" (expectation management):

System.Diagnostics.Process process = System.Diagnostics.Process.Start("cmd.exe");
while (!process.HasExited)
{
    //update UI
}
//done
查看更多
荒废的爱情
6楼-- · 2019-01-01 01:18

Use Process.WaitForExit? Or subscribe to the Process.Exited event if you don't want to block? If that doesn't do what you want, please give us more information about your requirements.

查看更多
有味是清欢
7楼-- · 2019-01-01 01:21

I had a case where Process.HasExited didn't change after closing the window belonging to the process. So Process.WaitForExit() also didn't work. I had to monitor Process.Responding that went to false after closing the window like that:

while (!_process.HasExited && _process.Responding) {
  Thread.Sleep(100);
}
...

Perhaps this helps someone.

查看更多
登录 后发表回答