【问题标题】:Redirecting standard output重定向标准输出
【发布时间】:2014-01-20 03:44:24
【问题描述】:

我有一个 C# 应用程序 A,它调用另一个 C# 应用程序 B,它调用多个应用程序实例 C。

我想将应用程序 C 的输出重定向到应用程序 A 的输出。

我已经将应用程序B 的输出重定向到应用程序A 的输出。

现在,在应用程序B 的代码中,我正在重定向每个进程的输出,并将重定向的输出打印到控制台。不幸的是,由于某种原因,没有打印任何内容。

(我目前正在测试它而不使用应用程序A - 我只运行应用程序B)。

代码如下:

private void runSingleFile (string execFile, string commandArgs)
{
      Process processToRun = new Process();
      processToRun .StartInfo.FileName = execFile;
      processToRun .StartInfo.Arguments = commandArgs;
      processToRun .StartInfo.UseShellExecute = false;
      processToRun .StartInfo.RedirectStandardOutput = true;
      processToRun .OutputDataReceived += outputRedirection;
      processToRun.Start();
      Console.WriteLine("");
      Thread.Sleep(100);  
      processToRun.BeginOutputReadLine();
}

private void outputRedirection(object sendingProcess, DataReceivedEventArgs outLine)
{
      try
      {
           if (outLine.Data != null)
              Console.WriteLine(outLine.Data);
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex.Message);
          return;
      }
}

有什么想法吗?

【问题讨论】:

  • 一些应用程序写入 StandardError 而不是 StandardOutput,您是否也尝试过重定向您的 StandardError?
  • 我确定应用程序 C 写入 StandardOutput,我写了它..无论如何,我现在尝试了 - 仍然没有。

标签: c# process output io-redirection


【解决方案1】:

.net Process 对象在正确处理 IO 方面并不容易“做正确的事”。

这些页面列出了一些需要处理的问题:

从您的代码示例中,您可以查看以下一些项目:

  1. 您需要同时捕获 stderr 和 stdin(如果不使用后者应立即关闭)。
  2. 您还需要注意,在子进程退出后,您仍然可以在outputRedirection 回调中接收事件。

【讨论】:

    【解决方案2】:

    您发布的代码有效。给定测试程序

    // ConsoleApplication2.exe
    static void Main(string[] args)
    {
        Console.WriteLine("Test1...");
        Console.WriteLine("Test2...");
        System.Threading.Thread.Sleep(100);
        Console.WriteLine("Test3...");
    }
    

    称为:

    static void Main(string[] args)
    {
        runSingleFile("ConsoleApplication2.exe", "");
        runSingleFile("ConsoleApplication2.exe", "");
        runSingleFile("ConsoleApplication2.exe", "");
        Console.ReadLine();
    }
    

    产生输出:

    很明显,其他代码在您的代码的另一部分中不起作用(您没有向我们展示的东西)。

    如果应用程序C 正常工作,最可能的解释是应用程序B 在C 的所有实例完成之前终止。您可能需要添加一些代码,使B 等待C 的所有实例返回。

    请注意:

     static void Main(string[] args)
     {
         runSingleFile("ConsoleApplication2.exe", "");
         runSingleFile("ConsoleApplication2.exe", "");
         runSingleFile("ConsoleApplication2.exe", "");
         //Console.ReadLine();   // ** Don't wait!
     }
    

    立即完成并且无法返回部分或全部数据(尤其是如果您删除了runSingleFile 中的Sleep 调用。

    考虑:

     static long processCount = 0;               //ADD
    
     static void runSingleFile(string execFile, string commandArgs)
     {
        Interlocked.Increment(ref processCount);  //ADD
        Process processToRun = new Process();
        processToRun.StartInfo.FileName = execFile;
        processToRun.StartInfo.Arguments = commandArgs;
        processToRun.StartInfo.UseShellExecute = false;
        processToRun.StartInfo.RedirectStandardOutput = true;
        processToRun.OutputDataReceived += outputRedirection;
        processToRun.EnableRaisingEvents = true;  //ADD
        processToRun.Exited += processExited;     //ADD
        processToRun.Start();
        Console.WriteLine("");
        processToRun.BeginOutputReadLine();
     }
    
     static void processExited(object sender, EventArgs e)
     {
         Interlocked.Decrement(ref processCount);
     }
    

    与

     static void Main(string[] args)
     {
         runSingleFile("ConsoleApplication2.exe", "");
         runSingleFile("ConsoleApplication2.exe", "");
         runSingleFile("ConsoleApplication2.exe", "");
         while (Interlocked.Read(ref processCount) > 0)
         {
            System.Threading.Thread.Sleep(100);
         }
     }
    

    然后上面的内容使应用程序B 等待,直到所有生成的进程都返回。这个例子很简单,显然可以改进,但我认为它说明了问题,并提供了解决方法。

    您可能很想使用像WaitHandle.WaitAll() 这样更优雅的东西,但这确实引入了 josh 指出的问题,即您的输出事件在进程终止之前可能不会触发 - 进程句柄将发出它已终止但它的信号已发布的消息可能仍在队列中。等待Exited 事件会整理该竞争条件,因为该事件将始终是队列中的最后一条消息(AFAIK)。

    另外,请注意这里使用 Interlocked 函数 - Console.WriteLine 是线程安全的,但其他变量访问不是。由于控制台应用程序中没有同步上下文,因此衍生进程引发的事件由线程池中的线程(不是控制台应用程序的主线程)处理。这引入了与多线程相关的所有问题,这些问题必须得到适当的管理。

    【讨论】:

      猜你喜欢
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多