pro*_*eek 3 c# process interprocess
正如本文所述,有两种方法可以在C#中调用另一个进程.
Process.Start("hello");
Run Code Online (Sandbox Code Playgroud)
和
Process p = new Process();
p.StartInfo.FileName = "hello.exe";
p.Start();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
Process.Start()?对于简单的情况,优点主要是方便.显然,你有更多选项(工作路径,在shell-exec之间选择等)和ProcessStartInfo路由,但是还有一个Process.Start(ProcessStartInfo)静态方法.
重新检查错误; Process.Start返回Process对象,因此您可以等待退出并在需要时检查错误代码.如果要捕获stderr,可能需要使用ProcessStartInfo方法.
使用第一种方法可能无法使用WaitForExit,因为如果进程已在运行,则该方法返回null.
如何检查新进程是否已启动,方法之间存在差异.第一个返回一个Process对象或null:
Process p = Process.Start("hello");
if (p != null) {
// A new process was started
// Here it's possible to wait for it to end:
p.WaitForExit();
} else {
// The process was already running
}
Run Code Online (Sandbox Code Playgroud)
第二个返回bool:
Process p = new Process();
p.StartInfo.FileName = "hello.exe";
bool s = p.Start();
if (s) {
// A new process was started
} else {
// The process was already running
}
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4908 次 |
| 最近记录: |