wasSharpNET – Blame information for rev 7
?pathlinks?
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.Text; |
||
9 | using System.Threading; |
||
10 | using wasSharp.Collections.Generic; |
||
11 | |||
12 | namespace wasSharpNET.Console |
||
13 | { |
||
14 | public class ConsoleSpin : IDisposable |
||
15 | { |
||
16 | private static readonly CircularQueue<string> spinArt = |
||
17 | new CircularQueue<string>(new[] {".oOo", "oOo.", "Oo.o", "o.oO"}); |
||
18 | |||
19 | private static readonly ManualResetEvent spinEvent = new ManualResetEvent(false); |
||
20 | private static Thread spinThread; |
||
21 | private static bool run = true; |
||
22 | |||
23 | public ConsoleSpin() |
||
24 | { |
||
25 | spinThread = new Thread(() => |
||
26 | { |
||
27 | do |
||
28 | { |
||
29 | spinEvent.WaitOne(); |
||
30 | Thread.Sleep(100); |
||
31 | |||
32 | var deco = spinArt.Dequeue(); |
||
33 | System.Console.Write(deco); |
||
34 | foreach (var c in deco) |
||
35 | System.Console.Write("\b"); |
||
36 | |||
37 | } while (run); |
||
38 | }) |
||
39 | { |
||
40 | IsBackground = true |
||
41 | }; |
||
42 | spinThread.Start(); |
||
43 | } |
||
44 | |||
45 | public void Dispose() |
||
46 | { |
||
47 | // Stop the callback thread. |
||
48 | try |
||
49 | { |
||
50 | run = false; |
||
51 | spinEvent.Set(); |
||
52 | if ((!spinThread.ThreadState.Equals(ThreadState.Running) && |
||
53 | !spinThread.ThreadState.Equals(ThreadState.WaitSleepJoin)) || spinThread.Join(1000)) return; |
||
54 | spinThread.Abort(); |
||
55 | spinThread.Join(); |
||
56 | } |
||
57 | catch (Exception) |
||
58 | { |
||
59 | /* We are going down and we do not care. */ |
||
60 | } |
||
61 | finally |
||
62 | { |
||
63 | spinThread = null; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public void Start() |
||
68 | { |
||
69 | spinEvent.Set(); |
||
70 | } |
||
71 | |||
72 | public void Stop() |
||
73 | { |
||
74 | spinEvent.Reset(); |
||
75 | } |
||
76 | } |
||
77 | } |