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