wasSharp – Diff between revs 21 and 25
?pathlinks?
Rev 21 | Rev 25 | |||
---|---|---|---|---|
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 Task CompletedTask = Task.FromResult(false); |
15 | private readonly Task CompletedTask = Task.FromResult(false); |
|
16 | private Task Delay; |
16 | private Task Delay; |
|
17 | private bool Disposed; |
17 | private bool Disposed; |
|
18 | |
18 | |
|
19 | private CancellationTokenSource tokenSource; |
19 | private CancellationTokenSource tokenSource; |
|
20 | |
20 | |
|
21 | public Timer() |
21 | public Timer() |
|
22 | { |
22 | { |
|
23 | } |
23 | } |
|
24 | |
24 | |
|
25 | public Timer(Action callback, TimeSpan dueTime, TimeSpan period) : this() |
25 | public Timer(Action callback, TimeSpan dueTime, TimeSpan period) : this() |
|
26 | { |
26 | { |
|
27 | Callback = callback; |
27 | Callback = callback; |
|
28 | DueTime = dueTime; |
28 | DueTime = dueTime; |
|
29 | Period = period; |
29 | Period = period; |
|
30 | |
30 | |
|
31 | Start(); |
31 | Start(); |
|
32 | } |
32 | } |
|
33 | |
33 | |
|
34 | public Timer(Action callback, int dueTime, int period) |
34 | public Timer(Action callback, int dueTime, int period) |
|
35 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
35 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
|
36 | { |
36 | { |
|
37 | } |
37 | } |
|
38 | |
38 | |
|
39 | public Timer(Action callback) : this(callback, TimeSpan.Zero, TimeSpan.Zero) |
39 | public Timer(Action callback) : this(callback, TimeSpan.Zero, TimeSpan.Zero) |
|
40 | { |
40 | { |
|
41 | } |
41 | } |
|
42 | |
42 | |
|
43 | public Timer(Action callback, double dueTime, int period) |
43 | public Timer(Action callback, double dueTime, int period) |
|
44 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
44 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
|
45 | { |
45 | { |
|
46 | } |
46 | } |
|
47 | |
47 | |
|
48 | public Timer(Action callback, uint dueTime, uint period) |
48 | public Timer(Action callback, uint dueTime, uint period) |
|
49 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
49 | : this(callback, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)) |
|
50 | { |
50 | { |
|
51 | } |
51 | } |
|
52 | |
52 | |
|
53 | public Action Callback { set; get; } |
53 | public Action Callback { set; get; } |
|
54 | |
54 | |
|
55 | public TimeSpan DueTime { get; set; } = TimeSpan.Zero; |
55 | public TimeSpan DueTime { get; set; } = TimeSpan.Zero; |
|
56 | |
56 | |
|
57 | public TimeSpan Period { get; set; } = TimeSpan.Zero; |
57 | public TimeSpan Period { get; set; } = TimeSpan.Zero; |
|
- | 58 | |
||
- | 59 | public DateTime ScheduledTime |
||
- | 60 | { |
||
- | 61 | get; |
||
- | 62 | set; |
||
- | 63 | } |
||
58 | |
64 | |
|
59 | public void Dispose() |
65 | public void Dispose() |
|
60 | { |
66 | { |
|
61 | // Stop the timer. |
67 | // Stop the timer. |
|
62 | Stop(); |
68 | Stop(); |
|
63 | |
69 | |
|
64 | // Dispose the token. |
70 | // Dispose the token. |
|
65 | if (tokenSource != null) |
71 | if (tokenSource != null) |
|
66 | { |
72 | { |
|
67 | tokenSource.Cancel(); |
73 | tokenSource.Cancel(); |
|
68 | tokenSource.Dispose(); |
74 | tokenSource.Dispose(); |
|
69 | tokenSource = null; |
75 | tokenSource = null; |
|
70 | } |
76 | } |
|
71 | |
77 | |
|
72 | // Set the disposed flag. |
78 | // Set the disposed flag. |
|
73 | Disposed = true; |
79 | Disposed = true; |
|
74 | } |
80 | } |
|
75 | |
81 | |
|
76 | private void Start() |
82 | private void Start() |
|
77 | { |
83 | { |
|
78 | // Check if we have an installed callback and that there is at least a due time. |
84 | // Check if we have an installed callback and that there is at least a due time. |
|
79 | if (Callback == null || DueTime.Equals(TimeSpan.Zero)) |
85 | if (Callback == null || DueTime.Equals(TimeSpan.Zero)) |
|
80 | return; |
86 | return; |
|
81 | |
87 | |
|
82 | // Dispose the previous token source. |
88 | // Dispose the previous token source. |
|
83 | if (tokenSource != null) |
89 | if (tokenSource != null) |
|
84 | { |
90 | { |
|
85 | tokenSource.Cancel(); |
91 | tokenSource.Cancel(); |
|
86 | tokenSource.Dispose(); |
92 | tokenSource.Dispose(); |
|
87 | tokenSource = null; |
93 | tokenSource = null; |
|
88 | } |
94 | } |
|
89 | |
95 | |
|
90 | // Create a new cancellation source. |
96 | // Create a new cancellation source. |
|
91 | tokenSource = new CancellationTokenSource(); |
97 | tokenSource = new CancellationTokenSource(); |
|
92 | |
98 | |
|
93 | Action tick = null; |
99 | Action tick = null; |
|
94 | |
100 | |
|
95 | tick = () => |
101 | tick = () => |
|
96 | { |
102 | { |
|
97 | Task.Run(Callback, tokenSource.Token); |
103 | Task.Run(Callback, tokenSource.Token); |
|
98 | if (Disposed) |
104 | if (Disposed) |
|
99 | return; |
105 | return; |
|
- | 106 | switch (!Period.Equals(TimeSpan.Zero)) |
||
- | 107 | { |
||
- | 108 | case true: |
||
- | 109 | ScheduledTime = DateTime.UtcNow; |
||
100 | Delay = !Period.Equals(TimeSpan.Zero) ? Task.Delay(Period, tokenSource.Token) : CompletedTask; |
110 | Delay = Task.Delay(DueTime, tokenSource.Token); |
|
- | 111 | break; |
||
- | 112 | default: |
||
- | 113 | Delay = CompletedTask; |
||
- | 114 | break; |
||
- | 115 | } |
||
101 | if (Disposed || Delay.IsCompleted) |
116 | if (Disposed || Delay.IsCompleted) |
|
102 | return; |
117 | return; |
|
103 | Delay.ContinueWith(o => tick(), tokenSource.Token); |
118 | Delay.ContinueWith(o => tick(), tokenSource.Token); |
|
104 | }; |
119 | }; |
|
- | 120 | |
||
- | 121 | switch (!DueTime.Equals(TimeSpan.Zero)) |
||
- | 122 | { |
||
- | 123 | case true: |
||
105 | |
124 | ScheduledTime = DateTime.UtcNow; |
|
- | 125 | Delay = Task.Delay(DueTime, tokenSource.Token); |
||
- | 126 | break; |
||
- | 127 | default: |
||
- | 128 | Delay = CompletedTask; |
||
- | 129 | break; |
||
106 | Delay = !DueTime.Equals(TimeSpan.Zero) ? Task.Delay(DueTime, tokenSource.Token) : CompletedTask; |
130 | } |
|
107 | if (Disposed || Delay.IsCompleted) |
131 | if (Disposed || Delay.IsCompleted) |
|
108 | return; |
132 | return; |
|
109 | Delay.ContinueWith(o => tick(), tokenSource.Token); |
133 | Delay.ContinueWith(o => tick(), tokenSource.Token); |
|
110 | } |
134 | } |
|
111 | |
135 | |
|
112 | public void Change(int dueTime, int period) |
136 | public void Change(int dueTime, int period) |
|
113 | { |
137 | { |
|
114 | Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)); |
138 | Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)); |
|
115 | } |
139 | } |
|
116 | |
140 | |
|
117 | public void Change(uint dueTime, int period) |
141 | public void Change(uint dueTime, int period) |
|
118 | { |
142 | { |
|
119 | Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)); |
143 | Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period)); |
|
120 | } |
144 | } |
|
121 | |
145 | |
|
122 | public void Change(TimeSpan dueTime, TimeSpan period) |
146 | public void Change(TimeSpan dueTime, TimeSpan period) |
|
123 | { |
147 | { |
|
124 | DueTime = dueTime; |
148 | DueTime = dueTime; |
|
125 | Period = period; |
149 | Period = period; |
|
126 | |
150 | |
|
127 | Start(); |
151 | Start(); |
|
128 | } |
152 | } |
|
129 | |
153 | |
|
130 | public void Stop() |
154 | public void Stop() |
|
131 | { |
155 | { |
|
132 | Change(0, 0); |
156 | Change(0, 0); |
|
133 | } |
157 | } |
|
134 | } |
158 | } |
|
135 | } |
159 | } |
|
136 | |
160 | |
|
137 |
|
161 |
|
|
138 | |
162 | |
|
139 | |
163 | |
|
140 | |
164 | |