2011-02-06 41 views
2

我正在使用第三方控制檯應用程序,它將數據定期逐行輸出到控制檯。當我試圖通過我的應用程序運行它以便我可以解析輸出數據時,我注意到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

回答

0

你也可以做這樣的:

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

0

更簡單的方法是使用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()); 
} 
1
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)); 
     } 
    } 
相關問題