wasSharpNET – Diff between revs 7 and 9
?pathlinks?
Rev 7 | Rev 9 | |||
---|---|---|---|---|
Line 3... | Line 3... | |||
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 | /////////////////////////////////////////////////////////////////////////// |
|
Line 6... | Line 6... | |||
6 | |
6 | |
|
7 | using System; |
- | ||
8 | using System.Text; |
7 | using System; |
|
9 | using System.Threading; |
8 | using System.Threading; |
|
Line 10... | Line 9... | |||
10 | using wasSharp.Collections.Generic; |
9 | using wasSharp.Collections.Generic; |
|
11 | |
10 | |
|
- | 11 | namespace wasSharpNET.Console |
||
- | 12 | { |
||
- | 13 | /////////////////////////////////////////////////////////////////////////// |
||
- | 14 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 15 | /////////////////////////////////////////////////////////////////////////// |
||
- | 16 | /// <summary> |
||
12 | namespace wasSharpNET.Console |
17 | /// A command-line spinner indicating activity. |
|
13 | { |
18 | /// </summary> |
|
14 | public class ConsoleSpin : IDisposable |
19 | public class ConsoleSpin : IDisposable |
|
15 | { |
20 | { |
|
Line 16... | Line 21... | |||
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 | |
|
Line -... | Line 24... | |||
- | 24 | private static readonly ManualResetEvent spinEvent = new ManualResetEvent(false); |
||
- | 25 | private static Thread spinThread; |
||
- | 26 | private static bool run = true; |
||
- | 27 | |
||
19 | private static readonly ManualResetEvent spinEvent = new ManualResetEvent(false); |
28 | public ConsoleSpin() : this(ConsoleExtensions.ConsoleTextAlignment.TOP_LEFT) |
|
20 | private static Thread spinThread; |
29 | { |
|
21 | private static bool run = true; |
30 | } |
|
22 | |
31 | |
|
23 | public ConsoleSpin() |
32 | public ConsoleSpin(ConsoleExtensions.ConsoleTextAlignment alignment) |
|
24 | { |
33 | { |
|
25 | spinThread = new Thread(() => |
34 | spinThread = new Thread(() => |
|
26 | { |
- | ||
27 | do |
- | ||
28 | { |
35 | { |
|
29 | spinEvent.WaitOne(); |
36 | while (run) |
|
30 | Thread.Sleep(100); |
37 | { |
|
31 | |
38 | spinEvent.WaitOne(); |
|
Line 32... | Line 39... | |||
32 | var deco = spinArt.Dequeue(); |
39 | var deco = spinArt.Dequeue(); |
|
- | 40 | deco.Write(alignment); |
||
33 | System.Console.Write(deco); |
41 | foreach (var c in deco) |
|
34 | foreach (var c in deco) |
42 | "\b".Write(alignment); |
|
35 | System.Console.Write("\b"); |
43 | |
|
36 | |
44 | Thread.Sleep(100); |
|
37 | } while (run); |
45 | } |
|
Line 45... | Line 53... | |||
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(); |
|
56 | } |
67 | } |
|
- | 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 |