【发布时间】:2010-03-11 14:34:16
【问题描述】:
我需要从我的代码中调用其他程序。
我需要等到它完成(同步调用)。
我该怎么做?
非常感谢。
【问题讨论】:
-
您对进程间通信感兴趣,是吗? en.wikipedia.org/wiki/Inter-process_communication
-
Web 服务和 WCF 服务都支持同步通信。 .Net 远程处理也是如此。
我需要从我的代码中调用其他程序。
我需要等到它完成(同步调用)。
我该怎么做?
非常感谢。
【问题讨论】:
尝试使用WaitForExit 方法。
Process p = new Process();
// Redirect the error stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "OtherProgram.exe";
p.StartInfo.Arguments = "My Arguments";
p.Start();
// Wait for the child process to exit.
p.WaitForExit();
【讨论】:
除非您显式进行异步调用,否则调用将是同步的(因此会阻塞当前线程)。执行这些操作的细节取决于您使用的所选应用程序到应用程序的通信机制。
这可能是:
还有更多。所有这些都允许您等待响应。
但具体情况非常不同(就像您使用每种方式时一样),因此如果没有更多关于您尝试实现的通信类型的详细信息,就无法更具体。
【讨论】: