2011-02-06 6 views
2

타사 콘솔 응용 프로그램을 사용하여 주기적으로 데이터를 콘솔에 한 줄씩 출력합니다. 출력 데이터를 구문 분석 할 수 있도록 내 응용 프로그램을 통해 실행하려고 시도했을 때 응용 프로그램이 종료 된 후에 만 ​​OutPutstream을 읽을 수 있다는 것을 알았습니다.콘솔 응용 프로그램이 주기적으로 출력을 플러시하지 않습니다.

5 초마다 콘솔에 뭔가를 출력하고 예상대로 작동하는 C# 콘솔 응용 프로그램으로 응용 프로그램을 테스트했습니다. 제가 말하는 제 3 자 프로세스는 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 –

답변

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

+0

@Omar - "나만 출력을 볼 수 있습니다 ..."라는게 무슨 뜻입니까? 이런 종류의 코드를 사용하면 리다이렉트 되었기 때문에 결과물을 보지 말아야한다. 아니면 무언가를 놓쳤는가? –

+0

나는 내가 생성 한 콘솔의 출력 만 볼 수 있으며, 응용 프로그램을 만들었습니다. – Omar

0

간단한 방법이 process 객체에 StandardOutput 객체를 사용하는 것입니다

아래는 내가 사용하는 코드입니다. 예제 코드 :

나는이에 대한 수정을 한
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()); 
} 
+0

작동하지 않습니다. 동일한 동작입니다. 나는'CreateNoWindow = false'를 설정하고 수동으로 창을 닫을 때만 출력을 봅니다. – Omar

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