控制台应用程序不会定期刷新输出

问题描述:

我正在使用第三方控制台应用程序,它将数据定期逐行输出到控制台。当我试图通过我的应用程序运行它以便我可以解析输出数据时,我注意到OutPutstream只在应用程序退出后才可读。控制台应用程序不会定期刷新输出

我用一个C#控制台应用程序测试了我的应用程序,该应用程序每5秒向控制台输出一些内容,并且按预期工作。我所调用的第三方进程是用Java或C++编写的(不确定),但它似乎不符合.NET期望的控制台应用程序标准。

是否有另一种方式来读取控制台应用程序输出的数据?

编辑:我从WPF应用程序调用该过程。因此需要异步读取。

编辑2:控制台应用程序从USB设备(加速度计 - http://www.gcdataconcepts.com/)读取数据。

下面是我使用的代码:

public void RunProcess() 
    { 
     Process process = new Process(); 
     process.StartInfo.FileName = "consoleApp.exe"; 

     process.StartInfo.UseShellExecute = false; 
     process.StartInfo.RedirectStandardOutput = true; 
     process.StartInfo.CreateNoWindow = true; 
     process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); 
     process.Start(); 
     process.BeginOutputReadLine(); 
    } 

    private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) 
    { 
     if (!string.IsNullOrEmpty(outLine.Data)) 
     { 
      Dispatcher.Invoke(new Action(() => 
      { 
       textBlock1.Text += outLine.Data + Environment.NewLine; 
      }), System.Windows.Threading.DispatcherPriority.Normal); 
     } 
    } 
+0

我已经做了修复这一点,你可以找到它的这种类似的问题... http://stackoverflow.com/a/11675360/361464 – 2012-07-26 16:28:07

你也可以做这样的:

public void RunProcess() 
{ 
    Process process = new Process(); 
    process.StartInfo.FileName = "consoleApp.exe"; 

    process.StartInfo.UseShellExecute = false; 
    process.StartInfo.RedirectStandardOutput = true; 
    process.StartInfo.CreateNoWindow = true; 
    process.Start(); 

    for (; ;) 
    { 
     string line = process.StandardOutput.ReadLine(); 
     if (line == null) 
      break; 

     Dispatcher.Invoke(new Action(() => 
      { 
       textBlock1.Text += outLine.Data + Environment.NewLine; 
      }), System.Windows.Threading.DispatcherPriority.Normal); 
    } 
    ... 
} 
+0

不工作。同样的行为。当我设置`CreateNoWindow = false`并手动关闭窗口时,我只能看到输出。 – Omar 2011-02-08 01:19:19

+0

@Omar - 你是什么意思“我只看到输出......”。有了这样的代码,你不应该看到输出,这是它的工作方式,因为它被重定向,或者我错过了什么? – 2011-02-08 07:16:14

更简单的方法是使用StandardOutput对象的process对象。示例代码:

Process process = new Process(); 
process.StartInfo.FileName = @"StackOverflowTest.exe"; 

process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardOutput = true; 
process.StartInfo.CreateNoWindow = true; 
process.Start(); 

while (!process.StandardOutput.EndOfStream) 
{ 
    Console.WriteLine("got: " + process.StandardOutput.ReadLine()); 
} 

protected virtual void StartProcess() { 
     // Start a new process for the cmd 
     process = new Process(); 
     process.StartInfo.UseShellExecute = false; 
     process.StartInfo.RedirectStandardOutput = true; 
     process.StartInfo.RedirectStandardError = true; 
     process.StartInfo.CreateNoWindow = true; 
     process.StartInfo.FileName = FileName; 
     process.StartInfo.Arguments = Arguments; 
     process.StartInfo.WorkingDirectory = WorkingDirectory; 
     process.Start(); 

     // Invoke stdOut and stdErr readers - each 
     // has its own thread to guarantee that they aren't 
     // blocked by, or cause a block to, the actual 
     // process running (or the gui). 
     new MethodInvoker(ReadStdOut).BeginInvoke(null, null); 
     new MethodInvoker(ReadStdErr).BeginInvoke(null, null); 

    } 

    /// <summary> 
    /// Handles reading of stdout and firing an event for 
    /// every line read 
    /// </summary> 
    protected virtual void ReadStdOut() { 
     string str; 
     while ((str = process.StandardOutput.ReadLine()) != null) 
     { 
      FireAsync(StdOutReceived, this, new DataReceivedEventArgs(str)); 
     } 
    } 

    /// <summary> 
    /// Handles reading of stdErr 
    /// </summary> 
    protected virtual void ReadStdErr() { 
     string str; 
     while ((str = process.StandardError.ReadLine()) != null) 
     { 
      FireAsync(StdErrReceived, this, new DataReceivedEventArgs(str)); 
     } 
    }