C# 如何启动一个新进程并等待它完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14692372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:41:13  来源:igfitidea点击:

How can I start a new Process and wait until it finishes?

c#process.start

提问by Paedow

I want to start a program with C# (could use Process.Start()). Then my program should wait until the started program is closed, before it continues.
How do I do this?

我想用 C# 启动一个程序(可以使用Process.Start())。然后我的程序应该等到启动的程序关闭,然后再继续。
我该怎么做呢?

采纳答案by Cédric Bignon

After you call Start()add: Process.WaitForExit()

调用后Start()添加:Process.WaitForExit()

 var myProcess = new Process {StartInfo = new ProcessStartInfo(processPath)};
 myProcess.Start().WaitForExit();

回答by Phillip Scott Givens

There are two mechanism. You can either hook the Process.Exited event or what you probably really want is to call Process.WaitForExit().

有两种机制。您可以挂钩 Process.Exited 事件,或者您可能真正想要的是调用 Process.WaitForExit()。

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx