如何同步运行进程,以相同的输出为目标?

Mik*_*ras 6 .net c# stdout process

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

此外,我希望所有进程都输出到同一个控制台窗口.事实上,他们似乎打开了自己的窗户.我确定我可以使用StandardOutput流写入控制台,但是如何禁止默认输出?

Are*_*ren 11

我相信你在寻找:

Process p = Process.Start("myapp.exe");
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)

输出:

StreamReader stdOut = p.StandardOutput;
Run Code Online (Sandbox Code Playgroud)

然后你像任何流阅读器一样使用它.

为了抑制窗口,它有点难:

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);
Run Code Online (Sandbox Code Playgroud)