2012-08-16 12 views
9

Esiste comunque un reindirizzamento dell'output standard di un processo generato e acquisirlo mentre si verifica. Tutto ciò che ho visto fa un ReadToEnd al termine del processo. Mi piacerebbe essere in grado di ottenere l'output mentre viene stampato.C# ottiene l'output del processo mentre è in esecuzione

Edit:

private void ConvertToMPEG() 
    { 
     // Start the child process. 
     Process p = new Process(); 
     // Redirect the output stream of the child process. 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     //Setup filename and arguments 
     p.StartInfo.Arguments = String.Format("-y -i \"{0}\" -target ntsc-dvd -sameq -s 720x480 \"{1}\"", tempDir + "out.avi", tempDir + "out.mpg"); 
     p.StartInfo.FileName = "ffmpeg.exe"; 
     //Handle data received 
     p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 
     p.Start(); 
    } 

    void p_OutputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     Debug.WriteLine(e.Data); 
    } 

risposta

13

Usa Process.OutputDataReceived evento dal processo, per ricevere i dati necessari.

Esempio:

var myProc= new Process(); 

...    
myProc.StartInfo.RedirectStandardOutput = true; 
myProc.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler); 

... 

private static void MyProcOutputHandler(object sendingProcess, 
      DataReceivedEventArgs outLine) 
{ 
      // Collect the sort command output. 
    if (!String.IsNullOrEmpty(outLine.Data)) 
    { 
     ....  
    } 
} 
+1

Sì, e inoltre è necessario impostare 'RedirectStandardOutput' su true affinché funzioni. – vcsjones

+0

@vcsjones: basta scrivere post aggiuntivo. – Tigran

+0

Come nella risposta [qui sopra] (http://stackoverflow.com/a/3642517/74757). –

3

Così, dopo un po 'più di scavo ho scoperto che ffmpeg utilizza stderr per l'output. Ecco il mio codice modificato per ottenere l'output.

 Process p = new Process(); 

     p.StartInfo.UseShellExecute = false; 

     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.RedirectStandardError = true; 

     p.StartInfo.Arguments = String.Format("-y -i \"{0}\" -target ntsc-dvd -sameq -s 720x480 \"{1}\"", tempDir + "out.avi", tempDir + "out.mpg"); 
     p.StartInfo.FileName = "ffmpeg.exe"; 

     p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); 
     p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 

     p.Start(); 

     p.BeginErrorReadLine(); 
     p.WaitForExit(); 
Problemi correlati