C
C#2w ago
Abdesol

How to handle outputs that alter the same line and remove lines while running a process?

Hello, I am trying to run a process in a C# program and the process output alters on the same line. Like, it shows percent, and then removes some lines and shows another line. Currently, using StandardOutput, I am not able to capture such scenarios. Is there any workaround to this? Thank you!
5 Replies
Pobiega
Pobiega2w ago
Yeah, you set the cursor position and write again.
Abdesol
Abdesol2w ago
I mean how do I set that?
async Task OutputToConsole(StreamReader reader, Process process)
{
var buffer = new char[1024];
while (true)
{
var read = await reader.ReadAsync(buffer, 0, buffer.Length);
if (read > 0)
{
await Console.Out.WriteAsync(new string(buffer, 0, read));
}
if (process.HasExited && read == 0) break;
}
}
async Task OutputToConsole(StreamReader reader, Process process)
{
var buffer = new char[1024];
while (true)
{
var read = await reader.ReadAsync(buffer, 0, buffer.Length);
if (read > 0)
{
await Console.Out.WriteAsync(new string(buffer, 0, read));
}
if (process.HasExited && read == 0) break;
}
}
This is the function in which I am printing the buffer I recv from stdout one of the processes that are not working well, forexample, is git clone command, it shows the first cloning text, after that it doesn't show the rest of the output (the progress, etc..) and finally terminates without any additional output
ComplexSymbol
ComplexSymbol2w ago
use Console.SetCursorPosition(int left, int top)
Abdesol
Abdesol2w ago
Ah.. i see.. then where do I get the cursor points to set accordingly?
ComplexSymbol
ComplexSymbol6d ago
I don't have all the code, but you can probably figure it out based on how many times youve written things.
Want results from more Discord servers?
Add your server
More Posts