wasSharpNET – Blame information for rev 27

Subversion Repositories:
Rev:
Rev Author Line No. Line
7 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.Threading;
9 using wasSharp.Collections.Generic;
10  
11 namespace wasSharpNET.Console
12 {
9 office 13 ///////////////////////////////////////////////////////////////////////////
14 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
15 ///////////////////////////////////////////////////////////////////////////
16 /// <summary>
17 /// A command-line spinner indicating activity.
18 /// </summary>
7 office 19 public class ConsoleSpin : IDisposable
20 {
21 private static readonly CircularQueue<string> spinArt =
27 office 22 new CircularQueue<string>(new[] {".oOo", "oOo.", "Oo.o", "o.oO"});
7 office 23  
24 private static readonly ManualResetEvent spinEvent = new ManualResetEvent(false);
25 private static Thread spinThread;
26 private static bool run = true;
27  
9 office 28 public ConsoleSpin() : this(ConsoleExtensions.ConsoleTextAlignment.TOP_LEFT)
7 office 29 {
9 office 30 }
31  
32 public ConsoleSpin(ConsoleExtensions.ConsoleTextAlignment alignment)
33 {
7 office 34 spinThread = new Thread(() =>
35 {
9 office 36 while (run)
7 office 37 {
38 spinEvent.WaitOne();
39 var deco = spinArt.Dequeue();
9 office 40 deco.Write(alignment);
7 office 41 foreach (var c in deco)
9 office 42 "\b".Write(alignment);
7 office 43  
9 office 44 Thread.Sleep(100);
45 }
7 office 46 })
47 {
48 IsBackground = true
49 };
50 spinThread.Start();
51 }
52  
53 public void Dispose()
54 {
55 // Stop the callback thread.
56 try
57 {
9 office 58 if (spinThread != null)
59 {
60 run = false;
61 spinEvent.Reset();
27 office 62 if (!spinThread.ThreadState.Equals(ThreadState.Running) &&
63 !spinThread.ThreadState.Equals(ThreadState.WaitSleepJoin) || spinThread.Join(1000))
9 office 64 return;
65 spinThread.Abort();
66 spinThread.Join();
67 }
7 office 68 }
69 catch (Exception)
70 {
71 /* We are going down and we do not care. */
72 }
73 finally
74 {
75 spinThread = null;
76 }
77 }
78  
79 public void Start()
80 {
81 spinEvent.Set();
82 }
83  
84 public void Stop()
85 {
86 spinEvent.Reset();
87 }
88 }
27 office 89 }