2011-08-08 39 views
2

使用命令行应用编码视频。该应用程序返回线,说:从命令行重新解析响应

%完成:34%

的媒体编码此更新。有没有办法使用流程类继续检查标准输出并将其传递回主执行脚本?我有一个启动过程的类,然后将标准输出写入到stringbuilder,但我想知道如何继续检查它。这是CURENT代码...

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

我记得你可以传入一个委托,作为StartInfo的一部分,当新数据到达时将被“CalledBack”调用。 – Jodrell

回答

0

可以使用Process.BeginOutputReadLine启动Process.OutputDataRecieved在触发事件。 UseShellExecute必须是falseRedirect<StreamOfChoice>Output必须为真,如在您的示例代码中。

MSDN上有一个例子,我不会在这里反胃。我注意到一些程序使用了我认为是意想不到的目的的不同流,因此对于来自不同流的事件使用相同的处理程序可能是合适的。

0

而不是使用“ReadToEnd”,在循环中使用几个字节(甚至一个字节)的“Read”。读取将阻塞,直到它读取您指定的字节数。找到正确的字节数,你应该能够从标准输出读取字符串。