0

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 
    }
}
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61
  • The rough outline you posted above, which is not the [mcve] that would be needed in order to answer the question properly, seems reasonable. The main thing is to make sure you handle both stdout and stderr separately, which the code seems to do. But it's obviously not real code, so it's entirely possible you deviated from correct code in some way that's not shown. The loop in particular is a red flag, as there's no obvious reason from your question why you would try to start the same process multiple times, nor any particular reason to expect that to work. – Peter Duniho Feb 18 '21 at 23:08
  • (Why you copy the `string` from the `DataReceivedEventArgs` to a `StringBuilder` only to convert it back to a `string` also is unclear, but...whatever) – Peter Duniho Feb 18 '21 at 23:09
  • @PeterDuniho i used this way to make sure that there is a "\r\n" on the end of every string. (i know that this is in the def of a string. but i come from the "C-World" where it could be that in a char array the "\r\n" is missing. – Mathias1988 Feb 20 '21 at 17:22
  • I don't see how the code you have above puts `"\r\n"` at the end of any string, never mind every one. The handler isn't even called until the `Process` class itself sees an end-of-line, and the EOL is stripped before the string is passed to the handler. All your handler does is copy the string into a `StringBuilder` and then copy it back out again. At no point is the string actually modified. In other words: while you may think you have a reason to implement the handler that way, the code doesn't do what you seem to be saying it does. – Peter Duniho Feb 20 '21 at 17:30

1 Answers1

0

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