I called an external console application from a WinForms application. When that application is run in console, it outputs its progress, and the progress line gets updated like an animation. But when I redirected it to WinForms, and appeneded the standard output to a text box, of course, all progress lines were appended. So, I thought I needed a way to get "update existing line, not add a new line" message.
To test this, I created a console application that updates the output line using an existing question. But with this console application, the WinForms application get only the first line "Downloading 0%..." and no more. I don't know what is wrong.
How to know in the WinForms app that "this line overwrites the last line"?
Child
namespace Child
{
class Program
{
public static void ClearCurrentConsoleLine()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
static void Main(string[] args)
{
for(int i = 0; i<=100; i+=10)
{
Console.WriteLine($"Downloading {i}%...");
Console.SetCursorPosition(0, Console.CursorTop - 1);
Thread.Sleep(200);
ClearCurrentConsoleLine();
}
}
}
Parent
namespace Parent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var si = new ProcessStartInfo();
si.RedirectStandardOutput = true;
si.CreateNoWindow = true;
si.FileName = "..\\net5.0\\Child.exe";
si.UseShellExecute = false;
var p = new Process();
p.StartInfo = si;
p.OutputDataReceived += P_OutputDataReceived;
p.Start();
p.BeginOutputReadLine();
}
void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.Invoke(new Action(() => {
textBox1.AppendText(e.Data);
}));
}
}
}