Search code examples
c#winformsvideo-processinghandbrake

Process output redirection batch stopps c#


I'm new at c# / winforms and try to batch convert video clips with handbrake. The convert itself is working when the processes are opened in an own windows without redirecting the Stdout/Stderr. But when I redirect the output to a winforms textbox only the first clip is converted. As I can see in the taskmanager the handbrake_Cli is already opened but doing nothing. I think that there is some STDerr/STDout in any buffer and is waiting to get flushed.... but I don't know how to do. Would be glad if anybody can give me a hint :-)

ProcessStartInfo info = new ProcessStartInfo(" \"" + textBoxHandbrakeCLI.Text + "\"");
process.StartInfo = info;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;

process.Exited += new EventHandler(process_Exited);
process.EnableRaisingEvents = true;
eventHandled = new TaskCompletionSource<bool>();

//Redirect output
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.OutputDataReceived += (sender, eventArgs) => MyProcOutputHandler(sender, eventArgs);
process.ErrorDataReceived  += (sender, eventArgs) => MyProcOutputHandler(sender, eventArgs);

foreach(pseudo)
{
    process.Start();
    if (!errorRedirect)
    {
        process.BeginErrorReadLine();
        process.BeginOutputReadLine();
        errorRedirect = true;
    }
}


 private void MyProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
 {
     System.Text.StringBuilder sb = new  System.Text.StringBuilder(outLine.Data);
     WriteStatus(sb.ToString());
 }
 
 
 public void WriteStatus(string value)
{
    if (InvokeRequired)
    {
        this.Invoke(new Action<string>(WriteStatus), new object[] { value});
        return;
    }
  
    try
    {
        textBoxAdvancedStatus.AppendText(Environment.NewLine + value);
    }
    catch
    {
        //stay calm 
    }
}

Solution

  • I got a workaround for my problem but still don´t know why i need it.

    foreach(pseudo) { 
    process.Start(); 
    process.BeginErrorReadLine(); 
    process.BeginOutputReadLine(); 
    process.WaitForExit(); 
    process.CancelErrorRead(); 
    process.CancelOutputRead(); 
    }