2

I've a WPF Application that I can launch with command line in a PowerShell like this :

MyProgram.exe -arg1 -arg2 | Out-Default

I need to write in this PowerShell a progress bar that overwrite itself at each step. There is my code :

public void PerformNextState()
{
    ++mStep;

    double nbRectangleToCompleteTheBar = 100.0;
    string prefix = "\r0% [";
    string suffix = "] 100%";

    StringBuilder str = new StringBuilder();

    str.Append(prefix);
    int nbRectangles = (int)Math.Round(nbRectangleToCompleteTheBar * mStep / mMaxItems);
    int i = 0;
    for (; i < nbRectangles; ++i)
    {
        str.Append('█');
    }
    for (; i < nbRectangleToCompleteTheBar; ++i)
    {
        str.Append(' ');
    }
    str.Append(suffix);

    Console.Write(str.ToString());
}

But the carriage return seems interpreted like a new line : enter image description here

As you can see the character █ is also misinterpreted.

I also tried with this syntax \x000D instead \r with the same results.

Furthermore, I tried to do it with Console.SetCursorPosition(), Console.CursorLeft and Console.CursorTop but I get IOException probably because it's a WPF Application and not Console Application.

I tried all these solutions in a Console Application and they all work. And I don't want to compile my WPF Application as Console Application because I think it's not a good practice and I will need to hide the console when launched with GUI.

  • 3
    Does this answer your question? [Progress bar in console application](https://stackoverflow.com/questions/24918768/progress-bar-in-console-application) – JosefZ Sep 17 '21 at 15:31
  • @JosefZ No. Unfortunately, all these solutions works on a Console Application, not on a WPF Application. Just a little example to understand kind of problem I face off. This code doesn't work on a WPF Application : `Console.Out.Write("test"); Console.Out.Flush();` The buffer is flushed only at the end of the process. – The Coyoyote Sep 20 '21 at 09:00
  • And I didn't said it before but I'm on .NET Framework 4.0. I don't know if there is any limitation with this. – The Coyoyote Sep 20 '21 at 12:26

0 Answers0