2012-04-23 143 views
0

嘗試調用「StandardOutput.ReadLine()」時應用程序掛起。應用程序凍結在「StandardOutput.ReadLine()」上。

代碼:

ProcessStartInfo startInfo = new ProcessStartInfo("c:\\windows\\system32\\myTesting.exe"); 
      String s = " "; 

      startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
      startInfo.CreateNoWindow = true; 
      startInfo.RedirectStandardInput = true; 
      startInfo.RedirectStandardOutput = true; 
      startInfo.UseShellExecute = false; 
      Process p = Process.Start(startInfo); 
      p.StandardInput.WriteLine("list volume\n"); 
      String f = ""; 

      while (!p.StandardOutput.EndOfStream) 
       { 
        s = p.StandardOutput.ReadLine(); 
       } 

「死鎖例外」 誤差有時會發生,但並非總是如此。

+0

http://stackoverflow.com/questions/2767496/standardoutput-endofstream-hangs – 2012-04-23 12:56:48

回答

0

perhabs the wihle is the problem。剛剛嘗試這一點:

// Create the child process. 
Process p = new Process(); 

// Redirect the output stream of the child process. 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 

//setting the application path 
p.StartInfo.FileName = "Write500Lines.exe"; 
p.Start(); 

// Do not wait for the child process to exit before 
// reading to the end of its redirected stream. 
// Read the output stream first and then wait. 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 
+0

您可以包括一些敘述說明你的代碼,所以OP知道你做了什麼,爲什麼它是一個答案他們的問題? – 2012-04-24 06:55:57