❔ Capturing process output

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.outputdatareceived?view=net-7.0#examples
I am using the following example to try and capture a process output and print it to a textbox at runtime. The process output is captured, but it is only printed after the process has terminated.
how do I make sure to print at runtime?

   var path = "C:\\program.exe";
  Process process = new Process();
            process.StartInfo.FileName = path;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
            {
                if (!String.IsNullOrEmpty(e.Data))
                {
                    output.Append(e.Data + "\r\n");
                }
            });
            process.Start();
            process.BeginOutputReadLine();
            process.WaitForExit();
            tboxConsole.AppendText(output.ToString());
            process.WaitForExit();
            process.Close();   
Occurs each time an application writes a line to its redirected StandardOutput stream.
Was this page helpful?