2011-08-08 5 views
2

명령 줄 앱을 사용하여 비디오를 인코딩합니다.명령 줄에서 응답을 다시 구문 분석합니다.

퍼센트 완료 : 미디어 인코딩으로이 업데이트됩니다

34 %이 앱라는 한 줄을 반환합니다. 프로세스 클래스를 사용하여 표준 출력을 계속 확인하고 기본 실행 스크립트로 다시 전달하는 방법이 있습니까? 프로세스를 시작한 클래스가 있고 표준 출력을 stringbuilder에 씁니다.하지만 계속 검사하는 방법을 알고 싶습니다. 이

public static Dictionary<string, string> StartProcess(string exePathArg, string argumentsArg, int timeToWaitForProcessToExit) 
    { 
     //the dictionary with the 
     Dictionary<string, string> retDirects = new Dictionary<string, string>(); 

     using (Process p = new Process()) 
     { 
      p.StartInfo.FileName = exePathArg; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.StartInfo.RedirectStandardError = true; 
      p.StartInfo.Arguments = argumentsArg; 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.CreateNoWindow = true; 

      try 
      { 

       p.Start(); 

       p.WaitForExit(timeToWaitForProcessToExit); 

       int exitCode; 
       try 
       { 
        exitCode = p.ExitCode; 

        StreamReader standardOutput = p.StandardOutput; 
        StreamReader standardError = p.StandardError; 

        retDirects.Add("StandardOutput", standardOutput.ReadToEnd()); 
        retDirects.Add("StandardError", standardError.ReadToEnd()); 
       } 
       catch { } 


      } 
      catch { } 
      finally 
      { 
       try 
       { 
        p.Kill(); 
        p.CloseMainWindow(); 
       } 
       catch { } 
      } 
     } 

     return retDirects; 
    } 
+0

새 데이터가 도착할 때 "CalledBack"이 될 StartInfo의 일부로 대리인을 전달할 수 있습니다. – Jodrell

답변

0

당신은 Process.OutputDataRecieved 이벤트의 발사를 시작하기 위해 Process.BeginOutputReadLine을 사용할 수 있습니다 ... curent 코드입니다. UseShellExecute은 예제 코드처럼 false이어야하며 Redirect<StreamOfChoice>Output이 true 여야합니다.

MSDN에 대한 예가 있습니다. 여기서 나는 반항하지 않을 것입니다. 일부 프로그램에서는 예상치 못한 용도로 생각한 다른 스트림을 사용하므로 다른 스트림의 이벤트에 대해 동일한 처리기를 사용하는 것이 적절할 수 있습니다.

0

"ReadToEnd"를 사용하는 대신 루프에서 수 바이트 (심지어 한 번에 한 바이트)의 "읽기"를 사용하십시오. 읽기는 지정한 바이트 수를 읽을 때까지 차단됩니다. 올바른 바이트 수를 찾으면 표준 출력에서 ​​문자열을 읽을 수 있어야합니다.

관련 문제