wasSharpNET
/Console/ConsoleSpin.cs |
@@ -5,12 +5,17 @@ |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.Text; |
using System.Threading; |
using wasSharp.Collections.Generic; |
|
namespace wasSharpNET.Console |
{ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// A command-line spinner indicating activity. |
/// </summary> |
public class ConsoleSpin : IDisposable |
{ |
private static readonly CircularQueue<string> spinArt = |
@@ -20,21 +25,24 @@ |
private static Thread spinThread; |
private static bool run = true; |
|
public ConsoleSpin() |
public ConsoleSpin() : this(ConsoleExtensions.ConsoleTextAlignment.TOP_LEFT) |
{ |
} |
|
public ConsoleSpin(ConsoleExtensions.ConsoleTextAlignment alignment) |
{ |
spinThread = new Thread(() => |
{ |
do |
while (run) |
{ |
spinEvent.WaitOne(); |
Thread.Sleep(100); |
|
var deco = spinArt.Dequeue(); |
System.Console.Write(deco); |
deco.Write(alignment); |
foreach (var c in deco) |
System.Console.Write("\b"); |
"\b".Write(alignment); |
|
} while (run); |
Thread.Sleep(100); |
} |
}) |
{ |
IsBackground = true |
@@ -47,12 +55,16 @@ |
// Stop the callback thread. |
try |
{ |
run = false; |
spinEvent.Set(); |
if ((!spinThread.ThreadState.Equals(ThreadState.Running) && |
!spinThread.ThreadState.Equals(ThreadState.WaitSleepJoin)) || spinThread.Join(1000)) return; |
spinThread.Abort(); |
spinThread.Join(); |
if (spinThread != null) |
{ |
run = false; |
spinEvent.Reset(); |
if ((!spinThread.ThreadState.Equals(ThreadState.Running) && |
!spinThread.ThreadState.Equals(ThreadState.WaitSleepJoin)) || spinThread.Join(1000)) |
return; |
spinThread.Abort(); |
spinThread.Join(); |
} |
} |
catch (Exception) |
{ |