2017-10-08 2 views
0

現在CMDファイルの内容を読み込もうとしていますが、動作していますが、cmdが終了するまでテキストをテキストボックスに追加しません。 この.BATファイルは、サーバーの起動と管理のためのものであり、cmdコンテンツは常に更新されています。 私がcmdファイルを閉じると、サーバが閉じてしまい、非常に良いと思われるので、サーバを実行する必要があります。 ここにジレンマがあります。Process.StandardOutput Property asyncをどのように読むのですか?

cmdが閉じるまで、テキストボックスにテキストを追加しません。

私はバックグラウンドワーカーに入れて非同期に実行しようとしましたが、同じことをします。 どのようにしてプロセスを読むのですか? StandardOutput非同期なので、私はデッドロックにならないでしょうか?

using System; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Linq; 
using System.Windows.Forms; 
using System.IO; 

namespace SendInput 
{ 
    public partial class Form1: Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     StreamReader outputReader = null; 
     StreamReader errorReader = null; 

     private void btnCommand_Click(object sender, EventArgs e) 
     { 
      CheckForIllegalCrossThreadCalls = false; 

      backgroundWorker1.RunWorkerAsync(); 
     } 

     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 
      try 
      { 
       //Create Process Start information 
       ProcessStartInfo processStartInfo = 
        new ProcessStartInfo(@"C:\Users\devPC\Desktop\Server\run.bat"); 
       processStartInfo.ErrorDialog = false; 
       processStartInfo.UseShellExecute = false; 
       processStartInfo.RedirectStandardError = true; 
       processStartInfo.RedirectStandardInput = true; 
       processStartInfo.RedirectStandardOutput = true; 

       //Execute the process 
       Process process = new Process(); 
       process.StartInfo = processStartInfo; 
       bool processStarted = process.Start(); 
       if (processStarted) 
       { 
        //Get the output stream 
        outputReader = process.StandardOutput; 
        errorReader = process.StandardError; 

        //Display the result 
        string displayText = "Output \n==============\n"; 
        displayText += outputReader.ReadToEnd(); 
        displayText += "\nError\n==============\n"; 
        displayText += errorReader.ReadToEnd(); 
        rtbConsole.Text = displayText; 
       } 
       process.WaitForExit(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
    } 
} 

答えて

0

これは、async/awaitを使用してプロセスを開始し、プロセス出力イベントを使用してUIを更新します。

using System; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Linq; 
using System.Windows.Forms; 
using System.IO; 

namespace SendInput 
{ 
    public partial class Form1: Form 
    { 
     private SynchronizationContext _syncContext; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private async void btnCommand_Click(object sender, EventArgs e) 
     { 
      CheckForIllegalCrossThreadCalls = false; 

      await StartProcessAsync(); 
     } 

     private Task StartProcessAsync() 
     { 
      return Task.Run(()=> 
      { 
       try 
       { 
        //Create Process Start information 
        ProcessStartInfo processStartInfo = 
         new ProcessStartInfo(@"C:\Users\devPC\Desktop\Server\run.bat"); 
        processStartInfo.ErrorDialog = false; 
        processStartInfo.UseShellExecute = false; 
        processStartInfo.RedirectStandardError = true; 
        processStartInfo.RedirectStandardInput = true; 
        processStartInfo.RedirectStandardOutput = true; 

        //Execute the process 
        Process process = new Process(); 
        process.StartInfo = processStartInfo; 

        process.OutputDataReceived += (sender, args) => UpdateText(args.Data); 
        process.ErrorDataReceived += (sender, args) => UpdateErrorText(args.Data); 

        process.Start(); 
        process.BeginOutputReadLine(); 
        process.BeginErrorReadLine(); 
        process.WaitForExit(); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      }); 
     } 

     private void UpdateText(string outputText) 
     { 
      _syncContext.Post(x => rtbConsole.AppendText("Output \n==============\n"), null);     
      _syncContext.Post(x => rtbConsole.AppendText(outputText), null); 
     } 

     private void UpdateErrorText(string outputErrorText) 
     { 
      _syncContext.Post(x => rtbConsole.AppendText("\nError\n==============\n"), null); 
      _syncContext.Post(x => rtbConsole.AppendText(outputErrorText), null); 
     } 
    } 
} 
関連する問題