CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

ConsoleSpinner to show progress in your console app

477 Views
Copy Code Show/Hide Line Numbers
public class ConsoleSpinner
{
    const int MaxSegmentIndex = 3;
 
    readonly char[] _segments = new [] {  '\\', '|', '/', '-'};
    readonly int _left;
    readonly int _top;
 
    int _curIndex;
 
    public ConsoleSpinner()
        : this(Console.CursorLeft, Console.CursorTop)
    {
    }
 
    public ConsoleSpinner(int left, int top)
    {
        _left = left;
        _top = top;
    }
 
    public void Update()
    {
        Console.CursorVisible = false;
        int curLeft = Console.CursorLeft;
        int curTop = Console.CursorTop;
 
        Console.SetCursorPosition(_left, _top);
        Console.Write(_segments[_curIndex++]);
        if (_curIndex > MaxSegmentIndex) _curIndex = 0;
 
        Console.SetCursorPosition(curLeft, curTop);
        Console.CursorVisible = true;
    }
}
 
 
// Simple demonstration of ConsoleSpinner
class Program
{
    static void Main(string[] args)
    {
        var spinner = new ConsoleSpinner();
        int i = 0;
        while (i < 50000)
        {
            spinner.Update();
            if (i++ % 1000 == 0)
                Console.Write('.');
        }
        Console.ReadLine();
        return;
    }
}
by Al Gonzalez
  May 18, 2010 @ 6:43am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate