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