wasSharp – Diff between revs 20 and 21
?pathlinks?
Rev 20 | Rev 21 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - 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 System.Threading.Tasks; |
9 | using System.Threading.Tasks; |
|
10 | |
10 | |
|
11 | namespace wasSharp.Timers |
11 | namespace wasSharp.Timers |
|
12 | { |
12 | { |
|
13 | public class Timer : IDisposable |
13 | public class Timer : IDisposable |
|
14 | { |
14 | { |
|
15 | private readonly Action callback; |
- | ||
16 | private readonly Task CompletedTask = Task.FromResult(false); |
15 | private readonly Task CompletedTask = Task.FromResult(false); |
|
17 | private Task Delay; |
16 | private Task Delay; |
|
18 | private bool Disposed; |
17 | private bool Disposed; |
|
19 | private TimeSpan dueTime; |
- | ||
20 | private TimeSpan period; |
- | ||
- | 18 | |
||
21 | private CancellationTokenSource tokenSource; |
19 | private CancellationTokenSource tokenSource; |
|
- | 20 | |
||
- | 21 | public Timer() |
||
- | 22 | { |
||
- | 23 | } |
||
22 | |
24 | |
|
23 | public Timer(Action callback, TimeSpan dueTime, TimeSpan period) |
25 | public Timer(Action callback, TimeSpan dueTime, TimeSpan period) : this() |
|
24 | { |
26 | { |
|
25 | this.callback = callback; |
27 | Callback = callback; |
|
26 | this.dueTime = dueTime; |
28 | DueTime = dueTime; |
|
27 | this.period = period; |
29 | Period = period; |
|
28 | |
30 | |
|
29 | Run(); |
31 | Start(); |
|
30 | } |
32 | } |
|
31 | |
33 | |
|
32 | public Timer(Action callback, int dueTime, int period) |
34 | public Timer(Action callback, int dueTime, int period) |
|
33 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
35 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
|
34 | { |
36 | { |
|
35 | } |
37 | } |
|
36 | |
38 | |
|
37 | public Timer(Action callback) : this(callback, TimeSpan.Zero, TimeSpan.Zero) |
39 | public Timer(Action callback) : this(callback, TimeSpan.Zero, TimeSpan.Zero) |
|
38 | { |
40 | { |
|
39 | } |
41 | } |
|
40 | |
42 | |
|
41 | public Timer(Action callback, double dueTime, int period) |
43 | public Timer(Action callback, double dueTime, int period) |
|
42 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
44 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
|
43 | { |
45 | { |
|
44 | } |
46 | } |
|
45 | |
47 | |
|
46 | public Timer(Action callback, uint dueTime, uint period) |
48 | public Timer(Action callback, uint dueTime, uint period) |
|
47 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
49 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
|
48 | { |
50 | { |
|
49 | } |
51 | } |
|
- | 52 | |
||
- | 53 | public Action Callback { set; get; } |
||
- | 54 | |
||
- | 55 | public TimeSpan DueTime { get; set; } = TimeSpan.Zero; |
||
- | 56 | |
||
- | 57 | public TimeSpan Period { get; set; } = TimeSpan.Zero; |
||
50 | |
58 | |
|
51 | public void Dispose() |
59 | public void Dispose() |
|
- | 60 | { |
||
- | 61 | // Stop the timer. |
||
- | 62 | Stop(); |
||
- | 63 | |
||
52 | { |
64 | // Dispose the token. |
|
53 | if (tokenSource != null) |
65 | if (tokenSource != null) |
|
54 | { |
66 | { |
|
55 | tokenSource.Cancel(); |
67 | tokenSource.Cancel(); |
|
56 | tokenSource.Dispose(); |
68 | tokenSource.Dispose(); |
|
57 | tokenSource = null; |
69 | tokenSource = null; |
|
58 | } |
70 | } |
|
- | 71 | |
||
- | 72 | // Set the disposed flag. |
||
59 | Disposed = true; |
73 | Disposed = true; |
|
60 | } |
74 | } |
|
61 | |
75 | |
|
62 | private void Run() |
76 | private void Start() |
|
- | 77 | { |
||
- | 78 | // Check if we have an installed callback and that there is at least a due time. |
||
- | 79 | if (Callback == null || DueTime.Equals(TimeSpan.Zero)) |
||
- | 80 | return; |
||
63 | { |
81 | |
|
64 | // Cancel any previous timer. |
82 | // Dispose the previous token source. |
|
65 | if (tokenSource != null) |
83 | if (tokenSource != null) |
|
66 | { |
84 | { |
|
67 | tokenSource.Cancel(); |
85 | tokenSource.Cancel(); |
|
68 | tokenSource.Dispose(); |
86 | tokenSource.Dispose(); |
|
69 | tokenSource = null; |
87 | tokenSource = null; |
|
70 | } |
88 | } |
|
71 | |
89 | |
|
72 | // Create a new cancellation source. |
90 | // Create a new cancellation source. |
|
73 | tokenSource = new CancellationTokenSource(); |
91 | tokenSource = new CancellationTokenSource(); |
|
74 | |
92 | |
|
75 | Action tick = null; |
93 | Action tick = null; |
|
- | 94 | |
||
76 | tick = () => |
95 | tick = () => |
|
77 | { |
96 | { |
|
78 | Task.Run(() => callback(), tokenSource.Token); |
97 | Task.Run(Callback, tokenSource.Token); |
|
79 | if (Disposed) |
98 | if (Disposed) |
|
80 | return; |
99 | return; |
|
81 | Delay = !period.Equals(0) ? Task.Delay(period, tokenSource.Token) : CompletedTask; |
100 | Delay = !Period.Equals(TimeSpan.Zero) ? Task.Delay(Period, tokenSource.Token) : CompletedTask; |
|
82 | if (Delay.IsCompleted) |
101 | if (Disposed || Delay.IsCompleted) |
|
83 | return; |
102 | return; |
|
84 | Delay.ContinueWith(t => tick(), tokenSource.Token); |
103 | Delay.ContinueWith(o => tick(), tokenSource.Token); |
|
85 | }; |
104 | }; |
|
86 | |
105 | |
|
87 | Delay = !dueTime.Equals(0) ? Task.Delay(dueTime, tokenSource.Token) : CompletedTask; |
106 | Delay = !DueTime.Equals(TimeSpan.Zero) ? Task.Delay(DueTime, tokenSource.Token) : CompletedTask; |
|
88 | if (Delay.IsCompleted) |
107 | if (Disposed || Delay.IsCompleted) |
|
89 | return; |
108 | return; |
|
90 | Delay.ContinueWith(t => tick(), tokenSource.Token); |
109 | Delay.ContinueWith(o => tick(), tokenSource.Token); |
|
91 | } |
110 | } |
|
92 | |
111 | |
|
93 | public void Change(int dueTime, int period) |
112 | public void Change(int dueTime, int period) |
|
94 | { |
113 | { |
|
95 | Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)); |
114 | Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)); |
|
96 | } |
115 | } |
|
97 | |
116 | |
|
98 | public void Change(uint dueTime, int period) |
117 | public void Change(uint dueTime, int period) |
|
99 | { |
118 | { |
|
100 | Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)); |
119 | Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)); |
|
101 | } |
120 | } |
|
102 | |
121 | |
|
103 | public void Change(TimeSpan dueTime, TimeSpan period) |
122 | public void Change(TimeSpan dueTime, TimeSpan period) |
|
104 | { |
123 | { |
|
105 | this.dueTime = dueTime; |
124 | DueTime = dueTime; |
|
106 | this.period = period; |
125 | Period = period; |
|
107 | |
126 | |
|
108 | Run(); |
127 | Start(); |
|
109 | } |
128 | } |
|
110 | |
129 | |
|
111 | public void Stop() |
130 | public void Stop() |
|
112 | { |
131 | { |
|
113 | Change(0, 0); |
132 | Change(0, 0); |
|
114 | } |
133 | } |
|
115 | } |
134 | } |
|
116 | } |
135 | } |
|
117 | |
136 | |
|
118 |
|
137 |
|
|
119 | |
138 | |
|
120 | |
139 | |
|
121 | |
140 | |