Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C#
Tip/Trick

A Console Progress Bar in C#

Rate me:
Please Sign up or sign in to vote.
4.95/5 (27 votes)
6 Jan 2020MIT1 min read 54.3K   1K   42   35
Easily add progress reporting to your console apps

ProgressDemo

Introduction

I write a lot of console apps, often tools and utilities that generate source code and other files. Sometimes, this generation can take some time and it would be nice to be able to report progress to the user.

I've seen a lot of progress bars for Winforms and WPF but not much of anything for the console, and yet it strikes me as something pretty useful.

Using this Mess

You can call ConsoleUtility.WriteProgressBar() to report a progress with a known amount of work to be done, or ConsoleUtility.WriteProgress() to report a progress with an unknown amount of work to be done. The first parameter in any case is the progress. For the open ended progress (WriteProgress()), the first parameter is simply an integer value that gets incremented each time. For the bar, it's a number between 0 and 100, inclusive. The second parameter should be false the first time the method is called, and true for subsequent times. This demo code should demonstrate:

C#
ConsoleUtility.WriteProgressBar(0);
for (var i = 0; i <= 100; ++i)
{
    ConsoleUtility.WriteProgressBar(i,true);
    Thread.Sleep(50);
}
Console.WriteLine();
ConsoleUtility.WriteProgress(0);
for (var i = 0; i <= 100; ++i)
{
    ConsoleUtility.WriteProgress(i, true);
    Thread.Sleep(50);
}

Coding this Mess

The code is short and sweet. The only non-intuitive bits are the use of backspace to overwrite our previous progress and the format string we use to pad the percentage with leading spaces.

C#
using System;

namespace CU
{
    static class ConsoleUtility
    {
        const char _block = '■';
        const string _back = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
        const string _twirl = "-\\|/";
        public static void WriteProgressBar(int percent, bool update = false)
        {
            if(update)
                Console.Write(_back);
            Console.Write("[");
            var p = (int)((percent / 10f)+.5f);
            for (var i = 0;i<10;++i)
            {
                if (i >= p)
                    Console.Write(' ');
                else
                    Console.Write(_block);
            }
            Console.Write("] {0,3:##0}%", percent);    
        }
        public static void WriteProgress(int progress, bool update = false)
        {
            if (update)
                Console.Write("\b");
            Console.Write(_twirl[progress % _twirl.Length]);
        }
    }
}

This is the code in its entirety, so you don't really even have to download the link. Just copy this if you like.

If you happen to write console utilities, I hope you find this useful.

History

  • 6th January, 2020 - Initial submission

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
United States United States
Just a shiny lil monster. Casts spells in C++. Mostly harmless.

Comments and Discussions

 
QuestionI love it! Pin
CoryLoriot28-Apr-22 10:31
CoryLoriot28-Apr-22 10:31 
AnswerRe: I love it! Pin
honey the codewitch28-Apr-22 11:20
mvahoney the codewitch28-Apr-22 11:20 
QuestionCasting Spells in C# Pin
David A. Gray11-Jun-20 11:43
David A. Gray11-Jun-20 11:43 
AnswerRe: Casting Spells in C# Pin
honey the codewitch11-Jun-20 14:25
mvahoney the codewitch11-Jun-20 14:25 
GeneralRe: Casting Spells in C# Pin
David A. Gray12-Jun-20 4:46
David A. Gray12-Jun-20 4:46 
GeneralRe: Casting Spells in C# Pin
honey the codewitch12-Jun-20 6:48
mvahoney the codewitch12-Jun-20 6:48 
GeneralRe: Casting Spells in C# Pin
David A. Gray12-Jun-20 8:44
David A. Gray12-Jun-20 8:44 
GeneralRe: Casting Spells in C# Pin
honey the codewitch12-Jun-20 9:43
mvahoney the codewitch12-Jun-20 9:43 
GeneralRe: Casting Spells in C# Pin
David A. Gray13-Jun-20 7:48
David A. Gray13-Jun-20 7:48 
GeneralRe: Casting Spells in C# Pin
honey the codewitch13-Jun-20 11:55
mvahoney the codewitch13-Jun-20 11:55 
GeneralRe: Casting Spells in C# Pin
David A. Gray17-Jun-20 4:46
David A. Gray17-Jun-20 4:46 
While I'm not sure what my libraries have to offer specifically around Unicode string conversion, I suspect you'll find a lot to like in it and it bigger companion, the GitHub - txwizard/WizardWrx_NET_API, a large part of which comes into a project that imports https://www.nuget.org/packages/WizardWrx.ConsoleAppAids3/. I suggest at some point that you have a look at the API documentation at WizardWrx .NET API, because a lot is packed into the eleven or so libraries that comprise the set.
David A. Gray
Delivering Solutions for the Ages, One Problem at a Time
Interpreting the Fundamental Principle of Tabular Reporting

QuestionHah, I just used this finally Pin
Marc Clifton29-Jan-20 8:05
mvaMarc Clifton29-Jan-20 8:05 
AnswerRe: Hah, I just used this finally Pin
honey the codewitch29-Jan-20 8:26
mvahoney the codewitch29-Jan-20 8:26 
QuestionThank You So Much! Pin
Goal Man7-Jan-20 19:44
Goal Man7-Jan-20 19:44 
AnswerRe: Thank You So Much! Pin
honey the codewitch7-Jan-20 21:52
mvahoney the codewitch7-Jan-20 21:52 
QuestionSome comments: Pin
Epsilon P7-Jan-20 5:34
Epsilon P7-Jan-20 5:34 
AnswerRe: Some comments: Pin
honey the codewitch7-Jan-20 6:28
mvahoney the codewitch7-Jan-20 6:28 
GeneralRe: Some comments: Pin
Epsilon P7-Jan-20 7:49
Epsilon P7-Jan-20 7:49 
GeneralRe: Some comments: Pin
honey the codewitch7-Jan-20 8:07
mvahoney the codewitch7-Jan-20 8:07 
GeneralRe: Some comments: Pin
Sacha Barber8-Jan-20 10:44
Sacha Barber8-Jan-20 10:44 
GeneralRe: Some comments: Pin
honey the codewitch8-Jan-20 11:18
mvahoney the codewitch8-Jan-20 11:18 
AnswerRe: Some comments: Pin
grantb410-Jan-20 5:48
grantb410-Jan-20 5:48 
GeneralRe: Some comments: Pin
honey the codewitch10-Jan-20 9:33
mvahoney the codewitch10-Jan-20 9:33 
QuestionA little improvement Pin
W. Kleinschmit7-Jan-20 2:17
W. Kleinschmit7-Jan-20 2:17 
AnswerRe: A little improvement Pin
honey the codewitch7-Jan-20 2:20
mvahoney the codewitch7-Jan-20 2:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.