Created
March 20, 2020 00:35
-
-
Save HeitorAugustoLN/6d22899d745331f2e7a9041bcb66214e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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