2016-03-29 23 views
1

私はC#winformのcコードを含むexeファイルを実行していますが、exeを完全に実行した後にのみcコードの完全な出力を得ます。私はexeの出力をWinformに同期的に(リアルタイムで行ごとに)リレーしたい。緩くthis exampleから構成されているプロセスのstdoutを同期的に読み込みます

var proc = new Process 
     { 
      StartInfo = new ProcessStartInfo 
      { 
       FileName = "background.exe", 
       Arguments = command, 
       UseShellExecute = false, 
       RedirectStandardOutput = true, 
       CreateNoWindow = true 
      } 
     }; 


     proc.Start(); 
     while (!proc.StandardOutput.EndOfStream) 
     { 
      ConsoleWindow.AppendText(proc.StandardOutput.ReadLine()); 
      ConsoleWindow.AppendText(Environment.NewLine); 

     } 
+0

'some c code'とはどういう意味ですか?実行中の*プロセスがそこに置かない限り、テキストは出力されません。 'background.exe'のコードをチェックしてください。 'background.exe'は実際にコンパイルされたバイナリ実行可能ファイルで、別の拡張子を持つCフ​​ァイルだけではないことを確認してください。 –

+0

これをチェックしましたか?[質問](http://stackoverflow.com/questions/285760/how- (net-stdout-in-net)とこの[question](http://stackoverflow.com/questions/18588659/redirect-process-output-c-sharp)のどちらを使用しても問題ありませんか? –

+0

すべてのコード例をチェックし、プロセスが終了した後にのみ出力を読み取ります。 –

答えて

0

これを試してみてください、:あなたはそれを確実にするために持っているので、コンソールからの出力を受け取り、イベントハンドラは、別のスレッドで実行されていることを

private void button1_Click(object sender, EventArgs e) 
    { 
     var consoleProcess = new Process 
     { 
      StartInfo = 
      { 
       FileName = 
        @"background.exe", 
       UseShellExecute = false, 
       RedirectStandardOutput = true 
      } 
     }; 

     consoleProcess.OutputDataReceived += ConsoleOutputHandler; 
     consoleProcess.StartInfo.RedirectStandardInput = true; 
     consoleProcess.Start(); 
     consoleProcess.BeginOutputReadLine(); 
    } 

    private void ConsoleOutputHandler(object sendingProcess, 
     DataReceivedEventArgs outLine) 
    { 
     // This is the method in your form that's 
     // going to display the line of output from the console. 
     WriteToOutput(outLine.Data); 
    } 

注意何でもフォーム上の出力をUIスレッドで表示するために使用します。

関連する問題