c# progress bar in console code example
Example 1: c# console progress bar
private static string Repeat(string str, int times) {
return string.Concat(Enumerable.Repeat(str, times));
}
private static void ProgressBar(int progress, int total, int chunks = 30, ConsoleColor completeColour = ConsoleColor.Green, ConsoleColor remainingColour = ConsoleColor.Gray, string symbol = "■", bool showPercent = true) {
Console.CursorLeft = 0;
Console.Write(" [");
Console.CursorLeft = chunks + 3;
Console.Write("]");
Console.CursorLeft = 3;
float chunk = (float) chunks / total;
double completeRaw = Math.Ceiling((double) chunk * progress);
int complete = (int) Math.Ceiling((double) chunk * progress);
int remaining = chunks - complete;
Console.ForegroundColor = completeColour;
Console.Write(Repeat(symbol, complete));
Console.ForegroundColor = remainingColour;
Console.Write(Repeat(symbol, remaining));
Console.CursorLeft = chunks + 4;
Console.ResetColor();
if (showPercent) {
int percent = (int) ((float) progress / (float) total * 100);
Console.Write($" {Repeat(" ", 3 - percent.ToString().Length)}{percent} %");
}
}
Example 2: add progress in console application c#
using System;
using System.Text;
using System.Threading;
public class ProgressBar : IDisposable, IProgress<double> {
private const int blockCount = 10;
private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
private const string animation = @"|/-\";
private readonly Timer timer;
private double currentProgress = 0;
private string currentText = string.Empty;
private bool disposed = false;
private int animationIndex = 0;
public ProgressBar() {
timer = new Timer(TimerHandler);
// A progress bar is only for temporary display in a console window.
// If the console output is redirected to a file, draw nothing.
// Otherwise, we'll end up with a lot of garbage in the target file.
if (!Console.IsOutputRedirected) {
ResetTimer();
}
}
public void Report(double value) {
// Make sure value is in [0..1] range
value = Math.Max(0, Math.Min(1, value));
Interlocked.Exchange(ref currentProgress, value);
}
private void TimerHandler(object state) {
lock (timer) {
if (disposed) return;
int progressBlockCount = (int) (currentProgress * blockCount);
int percent = (int) (currentProgress * 100);
string text = string.Format("[{0}{1}] {2,3}% {3}",
new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
percent,
animation[animationIndex++ % animation.Length]);
UpdateText(text);
ResetTimer();
}
}
private void UpdateText(string text) {
int commonPrefixLength = 0;
int commonLength = Math.Min(currentText.Length, text.Length);
while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength]) {
commonPrefixLength++;
}
StringBuilder outputBuilder = new StringBuilder();
outputBuilder.Append('\b', currentText.Length - commonPrefixLength);
outputBuilder.Append(text.Substring(commonPrefixLength));
int overlapCount = currentText.Length - text.Length;
if (overlapCount > 0) {
outputBuilder.Append(' ', overlapCount);
outputBuilder.Append('\b', overlapCount);
}
Console.Write(outputBuilder);
currentText = text;
}
private void ResetTimer() {
timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
}
public void Dispose() {
lock (timer) {
disposed = true;
UpdateText(string.Empty);
}
}
}
using System;
using System.Threading;
static class Program {
static void Main() {
Console.Write("Performing some task... ");
using (var progress = new ProgressBar()) {
for (int i = 0; i <= 100; i++) {
progress.Report((double) i / 100);
Thread.Sleep(20);
}
}
Console.WriteLine("Done.");
}
}