代码之家  ›  专栏  ›  技术社区  ›  Mike Pateras

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

  •  6
  • Mike Pateras  · 技术社区  · 14 年前

    我有一个.Net应用程序需要运行几个可执行文件。我使用的是Process类,但是Process.Start不会阻塞。我需要第一道工序在第二道工序之前完成。我该怎么做?

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

    1 回复  |  直到 5 年前
        1
  •  12
  •   Aren    14 年前

    我相信你在寻找:

    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);