windows C#同步进程启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1626094/
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
C# synchronous process starting
提问by tearman
I'm attempting to start a process from a piece of code but I want the code to pause execution until the process finishes and exits. Currently I'm using the System.Diagnostics.Process.Start() class to start (specifically) an uninstaller program, and the code executing afterwards does rely on the installer uninstaller finishing before it resumes execution.
我试图从一段代码开始一个进程,但我希望代码暂停执行,直到进程完成并退出。目前我正在使用 System.Diagnostics.Process.Start() 类来启动(特别是)卸载程序,之后执行的代码确实依赖于安装程序卸载程序在恢复执行之前完成。
Here's the code.
这是代码。
using System.Diagnostics;
var procStIfo = new ProcessStartInfo("cmd", "/c " + variableContainingUninstallerPath);
procStIfo.RedirectStandardOutput = true;
procStIfo.UseShellExecute = false;
procStIfo.CreateNoWindow = true;
var proc = new Process();
proc.StartInfo = procStIfo;
proc.Start();
回答by Reed Copsey
After your Start() call, add:
在您的 Start() 调用之后,添加:
proc.WaitForExit();
See Process.WaitForExitfor details.
有关详细信息,请参阅Process.WaitForExit。