✅ 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.
45 Replies
ero
ero2y ago
process.WaitForExit() does what it says
madogumglynium
madogumglyniumOP2y ago
what is that suppose to mean? i just tried not waiting for exit and i get no output at all
ero
ero2y ago
well sure you have to have both running kind of in parallel if you wait for the other process to exit, then your code will never read tboxConsole.AppendText before the process has exited but if you don't wait, then your code will reach tboxConsole.AppendText before any output has occurred
madogumglynium
madogumglyniumOP2y ago
i also tried capturing the process on a separate thread, but no luck. what would you suggest?
ero
ero2y ago
i would probably just change the textbox contents in the DataReceivedEventHandler
madogumglynium
madogumglyniumOP2y ago
lol i tried that too
ero
ero2y ago
and?
madogumglynium
madogumglyniumOP2y ago
no out put at all not even when the process has terminated is this what you mean?
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
// Use BeginInvoke to update the UI in the main thread
tboxConsole.BeginInvoke(new Action(() =>
{
tboxConsole.AppendText(e.Data + "\r\n");
}));
}
});
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
// Use BeginInvoke to update the UI in the main thread
tboxConsole.BeginInvoke(new Action(() =>
{
tboxConsole.AppendText(e.Data + "\r\n");
}));
}
});
ero
ero2y ago
something like that yeah
madogumglynium
madogumglyniumOP2y ago
well that didnt work unfortunatly :C
ZacharyPatten
ZacharyPatten2y ago
what kind of applicationis this? winforms?
ZacharyPatten
ZacharyPatten2y ago
if this is winforms, you probably need to call Refresh on the control to update it with the output: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.refresh?view=windowsdesktop-7.0
Control.Refresh Method (System.Windows.Forms)
Forces the control to invalidate its client area and immediately redraw itself and any child controls.
ZacharyPatten
ZacharyPatten2y ago
but you should also be multithreading this for example, try adding
DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
output.Append(e.Data + "\r\n");
this.Refresh(); // <- added this line
}
});
DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
output.Append(e.Data + "\r\n");
this.Refresh(); // <- added this line
}
});
madogumglynium
madogumglyniumOP2y ago
i tried having the refresh run on a separate thread but no luck..
madogumglynium
madogumglyniumOP2y ago
this is what i tried:
if (!String.IsNullOrEmpty(e.Data))
{
output.Append(e.Data + "\r\n");
this.BeginInvoke(new Action(() =>
{
tboxConsole.AppendText(output.ToString());
this.Refresh();
}));
}
if (!String.IsNullOrEmpty(e.Data))
{
output.Append(e.Data + "\r\n");
this.BeginInvoke(new Action(() =>
{
tboxConsole.AppendText(output.ToString());
this.Refresh();
}));
}
ZacharyPatten
ZacharyPatten2y ago
it sounds like it should be working then based on your comments. I don't think I can help without being able to replicate your issue. If you can share your code so we can test it for ourselves please do
madogumglynium
madogumglyniumOP2y ago
I am pushing to github atm, https://github.com/karim-daman/Replication_Test.git really appretiate your attempt to help. NOTE: adjust the exe path so that the form can reference it.
GitHub
GitHub - karim-daman/Replication_Test
Contribute to karim-daman/Replication_Test development by creating an account on GitHub.
ero
ero2y ago
you gotta be trippin if you think anyone's just gonna launch this exe
madogumglynium
madogumglyniumOP2y ago
ur not suppose to be launching it lol i mean u can but the form would launch it when i hit start it's a published .netcore web api as an exe you can use a vm if ur sus
ero
ero2y ago
i mean you have to start it to do your replication
madogumglynium
madogumglyniumOP2y ago
no u dont u run the form, then click start, and the form would run the exe as a separate process
ero
ero2y ago
...which runs the exe which is what i'm saying
madogumglynium
madogumglyniumOP2y ago
i was clear about this since day one lol
ero
ero2y ago
anyway it works fine
madogumglynium
madogumglyniumOP2y ago
what? u see output? at run time?
ero
ero2y ago
yeah
madogumglynium
madogumglyniumOP2y ago
wtf no way
ero
ero2y ago
well no, i made changes
madogumglynium
madogumglyniumOP2y ago
show me!
ero
ero2y ago
cause your code still didn't make sense
using Process process = new()
{
StartInfo = new()
{
FileName = /* */,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};

process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;

BeginInvoke(() =>
{
tboxConsole.AppendText(e.Data + Environment.NewLine);
tboxConsole.Refresh();
});
});

process.Start();
process.BeginOutputReadLine();
using Process process = new()
{
StartInfo = new()
{
FileName = /* */,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};

process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;

BeginInvoke(() =>
{
tboxConsole.AppendText(e.Data + Environment.NewLine);
tboxConsole.Refresh();
});
});

process.Start();
process.BeginOutputReadLine();
madogumglynium
madogumglyniumOP2y ago
i dont see any output on my side..
ero
ero2y ago
show your code now
madogumglynium
madogumglyniumOP2y ago
using Process process = new()
{
StartInfo = new()
{
FileName = path,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};

process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;

BeginInvoke(() =>
{
tboxConsole.AppendText(e.Data + Environment.NewLine);
tboxConsole.Refresh();
});
});

process.Start();
process.BeginOutputReadLine();
using Process process = new()
{
StartInfo = new()
{
FileName = path,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};

process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
return;

BeginInvoke(() =>
{
tboxConsole.AppendText(e.Data + Environment.NewLine);
tboxConsole.Refresh();
});
});

process.Start();
process.BeginOutputReadLine();
ero
ero2y ago
just push to the repo maybe good one i guess the using statement actually breaks it. makes sense
madogumglynium
madogumglyniumOP2y ago
OMGOSH BRO UR A GENIOUS! @just_ero THANKS!!!
ero
ero2y ago
make sure to kill the process when your app shuts down
madogumglynium
madogumglyniumOP2y ago
is there a more graceful way of shutting down the process?
ero
ero2y ago
don't think you need one
madogumglynium
madogumglyniumOP2y ago
@ZP ░▒▓█├■̶˾̶͞■┤█▓▒░ Thanks Zach!
ero
ero2y ago
maybe clear the textbox when the api process is stopped make the textbox readonly and your info groupbox doesn't scale
madogumglynium
madogumglyniumOP2y ago
i am going to look into that , i noiticed it (idk how to fix that) also, Gutter Country is pretty dope
ero
ero2y ago
It's amazing
madogumglynium
madogumglyniumOP2y ago
so raw!
ero
ero2y ago
I didn't create it if you think that Also $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server