© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
71 replies
madogumglynium

✅ 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();   
  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();   
Process.OutputDataReceived Event (System.Diagnostics)
Occurs each time an application writes a line to its redirected StandardOutput stream.
Process.OutputDataReceived Event (System.Diagnostics)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Capturing process output via OutputDataReceived event
C#CC# / help
3y ago
❔ Capturing process output
C#CC# / help
3y ago
standard output from Process not giving any output for certain commands
C#CC# / help
2y ago
Next page