2016-12-21 7 views
0

Process 클래스를 사용하여 tasklist.exe를 실행하고 RedirectStandardOutput을 true로 설정하려고하면 프로세스가 종료되지 않습니다.표준 출력을 리디렉션 할 때 TaskList.exe가 끝나지 않는 이유는 무엇입니까?

using System.Diagnostics; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     RunProcess("tasklist.exe"); 
    } 

    private static void RunProcess(string command) 
    { 
     var process = new Process() 
     { 
      StartInfo = 
      { 
       FileName = command, 
       RedirectStandardOutput = true, 
       UseShellExecute = false 
      } 
     }; 

     process.Start(); 
     process.WaitForExit(); 
    } 
} 

RedirectStandardOutput을 false로 설정하면 프로세스가 종료됩니다 !!!

tasklist.exe 프로세스가 절대로 종료되지 않는 이유는 무엇입니까? Windows 7과 .NET Framework 4.5.2를 사용하고 있습니다.

나는 tasklist.exe를 강제 종료 할 때마다 정확히 4096 바이트가 표준 출력에 기록된다는 것을 알았습니다! 크기를 늘려야하는 문자 버퍼가 있습니까?

+2

당신은 출력을 읽지 않는를 .. – Blorgbeard

+0

왜 그 문제를합니까? – LVBen

+0

WaitForExit 다음에이 두 줄을 쉽게 추가 할 수 있지만 실행되지 않습니다. var response = process.StandardOutput.ReadToEnd(); Console.WriteLine (response); – LVBen

답변

2

당신은 당신의 코드에이 줄을 추가 RedirectStandardOutput = true를 사용하는 경우 :

process.Start(); 

// To avoid deadlocks, always read the output stream first and then wait. 
string out = process.StandardOutput.ReadToEnd(); 

process.WaitForExit(); 
+1

StandardError 대신 StandardOutput을 사용하는 것이 좋겠다고 생각합니다. 그렇습니다. – LVBen

+0

감사합니다. LVBen;) –