Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A Console Progress Bar in C#

4.95/5 (28 votes)
6 Jan 2020MIT1 min read 59.3K   1.1K  
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