Skip to content

Instantly share code, notes, and snippets.

@XoseLluis
Created April 20, 2016 21:56
Show Gist options
  • Save XoseLluis/48da5512cb1fb38ff84c9bb432684f72 to your computer and use it in GitHub Desktop.
Save XoseLluis/48da5512cb1fb38ff84c9bb432684f72 to your computer and use it in GitHub Desktop.
A ConsoleSpinner in C# (you know the typical |/-\|/-\ kind of thing)
using System;
using System.Collections.Generic;
namespace HttpClientAsyncTest
{
public class ConsoleSpinner
{
int pos;
List<string> items;
public ConsoleSpinner(List<string> items)
{
this.items = items;
this.pos = -1;
}
private void ClearAndReposition(int length)
{
for(int i=0; i<length; i++)
{
Console.Write("\b \b"); //move back, remove by writing ' ', move back again
}
}
public void WriteNext()
{
if (this.pos != -1)
this.ClearAndReposition(this.items[this.pos].Length);
if (++this.pos >= this.items.Count)
this.pos = 0;
Console.Write(this.items[this.pos]);
}
}
}
//var consoleSpinner = new ConsoleSpinner(new List<string>(){"|", "/", "-", "\\", "|", "/", "-", "\\"});
var consoleSpinner = new ConsoleSpinner(new List<string>(){"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"});
while(true)
{
consoleSpinner.WriteNext();
Thread.Sleep(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment