0

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);
            }));
        }
    }
}
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
  • Perhaps the output contains backspace or carriage return characters that cause the cursor to move back (to the line start) so new text overwrites old. Try instead doing `textBox1.AppendText(e.Data.Replace("\b", "BKSP").Replace("\r", "CR"));` and see if your textbox gains BKSP/CR strings - if so look for \r or \b in your logic – Caius Jard Apr 12 '21 at 18:58
  • `Console.SetCursorPosition` only has an effect when displayed in a console window, not when output is redirected. – Klaus Gütter Apr 12 '21 at 18:58
  • @CaiusJard I tried that, but the output seems to contain none of the two. The console application I meant is [youtube-dl](https://github.com/ytdl-org/youtube-dl). – Damn Vegetables Apr 12 '21 at 19:10
  • @KlausGütter I guess so. But that is just for my testing, and the actual console application is not written in C#. I guess it is Python or something. So, I was looking for a generic way to do this with any Windows console application that updates existing lines. – Damn Vegetables Apr 12 '21 at 19:12

0 Answers0