❔ 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.
2 Replies
Jester
Jester2y ago
process.WaitForExit(); // waiting for process to exit
tboxConsole.AppendText(output.ToString()); // showing the output
process.WaitForExit(); // waiting for process to exit
tboxConsole.AppendText(output.ToString()); // showing the output
its exactly doing what you coded it to do. the following code may work but might have threading issues
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
output.Append(e.Data + "\r\n");
tboxConsole.Text = output.ToString();
}
});
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
output.Append(e.Data + "\r\n");
tboxConsole.Text = output.ToString();
}
});
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server