2012-08-16 20 views
9

Czy jest jednak tak, aby przekierować standardowe wyjście zrodził proces i uchwycić go jako dzieje. Wszystko, co widziałem, po prostu robi ReadToEnd po zakończeniu procesu. Chciałbym móc uzyskać dane wyjściowe w trakcie drukowania.C# uzyskać dane wyjściowe procesu podczas pracy

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); 
    } 

Odpowiedz

13

Zastosowanie Process.OutputDataReceived wydarzenie z procesu, aby otrzymywać potrzebne dane.

Przykład:

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

Tak, a ponadto należy ustawić opcję 'RedirectStandardOutput' na wartość true, aby działała. – vcsjones

+0

@vcsjones: wystarczy wkręcić dodatkowy wpis. – Tigran

+0

Jak w odpowiedzi [tutaj] (http://stackoverflow.com/a/3642517/74757). –

3

Więc po trochę więcej kopania I okazało się, że używa ffmpeg stderr do wyjścia. Oto mój zmodyfikowany kod, aby uzyskać dane wyjściowe.

 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(); 
Powiązane problemy