2014-10-29 25 views
0

我想在richTextBox中重定向Process的標準輸出。這裏是我的工藝配置,C#進程RedirectStandardOutput不重定向

 string command = "/K perl C:\\Server.pl "; 

     ProcessStartInfo startInfo = new ProcessStartInfo(); 
     Process proc = new Process(); 
     startInfo.WindowStyle = ProcessWindowStyle.Normal; 
     startInfo.FileName = "cmd.exe"; 
     startInfo.Arguments = command; 
     startInfo.UseShellExecute = false; 
     startInfo.RedirectStandardOutput = true; 

     proc.StartInfo = startInfo; 
     proc.OutputDataReceived += (s, ea) => this.richTextBox1.AppendText(ea.Data); 
     proc.Start(); 
     proc.BeginOutputReadLine(); 

這裏是我的Server.pl文件

 print "Server1 \n"; 

     while(1) 
     { 
      print "Server \n"; 
      sleep 1; 
     } 

但是當我運行該程序的cmd.exe僅僅是黑色,並且沒有在RichTextBox的印刷。但是當我改變

 startInfo.RedirectStandardOutput = false; 

我有這一點把我的cmd.exe:

 Server1 
     Server 
     Server 
     Server 
     ... 

我如何解決這個問題?

+0

我知道這是可能的運行'perl.exe',而不是'cmd.exe',但基於某些原因,我需要讓這個解決方案工作,CMD.EXE – 2014-10-29 14:23:15

回答

2

可能就像禁用perl腳本中的輸出緩衝一樣簡單。這是使用$|特殊變量完成的(請參閱perlvar)。

$| = 1; 
print "Server1 \n"; 
... 
+0

是的,你是對的。這很簡單。但我不明白的是爲什麼通過(startInfo.RedirectStandardOutput = false;)Perl不做輸出緩衝?! – 2014-10-29 15:46:34