2013-05-11 26 views
2

結果。process.OutputDataReceived不是從CMD</p> <pre><code>C:UsersXXXXX>adb start-server * daemon not running. starting it now * * daemon started successfully * C:UsersXXXXX> </code></pre> <p>我的C#代碼讀取所有行

public string devicesPlus() 
{ 
    psi.Arguments = "start-server"; 
    call = Process.Start(psi); 
    call.OutputDataReceived += new DataReceivedEventHandler(call_OutputDataReceived); 
    call.ErrorDataReceived += new DataReceivedEventHandler(call_OutputDataReceived); 
    call.EnableRaisingEvents = true; 
    call.Exited += new EventHandler(call_Exited); 
    call.Start(); 
    call.BeginOutputReadLine(); 
    call.BeginErrorReadLine(); 
    call.StandardInput.Close(); 
    call.WaitForExit(); 
    return outData.ToString(); 
} 

private void call_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    if (e.Data != null) 
    { 
     outData.Append(e.Data); 
    } 
} 

現在,當我打電話給devicesPlus時,有一段時間我只有*守護進程沒有運行。現在開始它* 有一段時間,它只是在後臺工作,沒有結果.. 你能告訴我什麼是錯誤的代碼,爲什麼我沒有得到正確的返回像cmd .. 新的C#對不起英語不好。 ..

更新 如果我從我的應用程序外殺死adb我突然從我的軟件得到答覆。

+0

的代碼不能工作張貼的UseShellExecute屬性賦值丟失。不使用CreateNoWindow並且不重定向輸入是有風險的。很明顯,如果你沒有發佈準確的代碼,你就無法得到準確的答案。 – 2013-05-11 09:54:04

+0

我知道它的先生,psi是抽象類的一部分.. – user1965804 2013-05-11 10:01:14

回答

3

添加這些行也可以參考StandardError stream

psi.Arguments = "start-server"; 
psi.UseShellExecute = false; 
psi.RedirectStandardError = true; 
psi.RedirectStandardOutput = true; 

call = Process.Start(psi); 
call.OutputDataReceived += new DataReceivedEventHandler(call_OutputDataReceived); 
call.ErrorDataReceived += new DataReceivedEventHandler(call_OutputDataReceived); 
call.Exited += new EventHandler(call_Exited); 

call.EnableRaisingEvents = true; 

call.BeginOutputReadLine(); 
call.BeginErrorReadLine(); 
call.StandardInput.Close(); 

call.WaitForExit(); 

return outData.ToString(); 
+0

相同的先生沒有發生與此.. – user1965804 2013-05-11 09:41:11

+0

我已經在ProcessStartInfo中添加了一些設置,刪除了額外的電話開始..給這個試試.. – rene 2013-05-11 10:03:45

+0

我更新後plz幫助先生.. – user1965804 2013-05-11 13:03:23

2

WaitForExit()只等待進程退出。它不會等待您的流程接收所有輸出,因此您在那裏遇到競爭情況。

call_OutputDataReceived將被調用e.Data == null來表示輸出結束。在使用outData.ToString()之前,您需要等待該呼叫。

例如,你可以使用一個new CountdownEvent(2)等待兩個流的末尾:

CountdownEvent countdownEvent; 

    public string devicesPlus() 
    { 
     psi.Arguments = "start-server"; 
     countdownEvent = new CountdownEvent(2); 
     call = Process.Start(psi); 
     call.OutputDataReceived += new DataReceivedEventHandler(call_OutputDataReceived); 
     call.ErrorDataReceived += new DataReceivedEventHandler(call_OutputDataReceived); 
     call.EnableRaisingEvents = true; 
     call.Exited += new EventHandler(call_Exited); 
     call.Start(); 
     call.BeginOutputReadLine(); 
     call.BeginErrorReadLine(); 
     call.StandardInput.Close(); 
     call.WaitForExit(); 
     countdownEvent.Wait(); 
     return outData.ToString(); 
    } 

    private void call_OutputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     if (e.Data != null) 
     { 
      // prevent race condition when data is received form stdout and stderr at the same time 
      lock (outData) 
      { 
       outData.Append(e.Data); 
      } 
     } 
     else 
     { 
      // end of stream 
      countdownEvent.AddCount(); 
     } 
    } 
+0

親愛的先生謝謝你的回覆,我嘗試着,但沒有發生,如果我殺死從我的C#應用​​程序以外的ADB,我突然得到答覆,我未能修復它.. – user1965804 2013-05-11 12:51:38

相關問題