2010-06-22 30 views
5

我有一个需要运行多个可执行文件的.Net应用程序。我正在使用Process类,但Process.Start不会阻塞。我需要在第二次运行之前完成第一个过程。我怎样才能做到这一点?如何同步运行进程,目标是相同的输出?

此外,我想所有的进程都输出到相同的控制台窗口。事实上,他们似乎打开了自己的窗户。我确定我可以使用StandardOutput流写入控制台,但我怎样才能抑制默认输出?

回答

10

我相信你正在寻找:

Process p = Process.Start("myapp.exe"); 
p.WaitForExit(); 

对于输出:

StreamReader stdOut = p.StandardOutput; 

然后你使用它像任何流读取。

为了抑制这是一个有点困难窗口:

ProcessStartInfo pi = new ProcessStartInfo("myapp.exe"); 
pi.CreateNoWindow = true; 
pi.UseShellExecute = true; 

// Also, for the std in/out you have to start it this way too to override: 
pi.RedirectStandardOutput = true; // Will enable .StandardOutput 

Process p = Process.Start(pi); 
+0

完美!有关输出的任何想法? – 2010-06-22 18:52:50

+0

更新了输出。 – Aren 2010-06-22 18:56:22

+0

但是,我应该如何取消为每个进程弹出的控制台窗口? – 2010-06-22 19:01:18

相关问题