Skip to content

Instantly share code, notes, and snippets.

@HeitorAugustoLN
Created March 20, 2020 00:35
Show Gist options
  • Save HeitorAugustoLN/6d22899d745331f2e7a9041bcb66214e to your computer and use it in GitHub Desktop.
Save HeitorAugustoLN/6d22899d745331f2e7a9041bcb66214e to your computer and use it in GitHub Desktop.
public class Spinner : IDisposable
{
private const string Sequence = @"/-\|";
private int counter = 0;
private readonly int left;
private readonly int top;
private readonly int delay;
private bool active;
private readonly Thread thread;
public Spinner(int left, int top, int delay = 100)
{
this.left = left;
this.top = top;
this.delay = delay;
thread = new Thread(Spin);
}
public void Start()
{
active = true;
if (!thread.IsAlive)
thread.Start();
}
public void Stop()
{
active = false;
Draw(' ');
}
private void Spin()
{
while (active)
{
Turn();
Thread.Sleep(delay);
}
}
private void Draw(char c)
{
Console.SetCursorPosition(left, top);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(c);
}
private void Turn()
{
Draw(Sequence[++counter % Sequence.Length]);
}
public void Dispose()
{
Stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment