-
Notifications
You must be signed in to change notification settings - Fork 3
/
Timer.cs
220 lines (198 loc) · 6.94 KB
/
Timer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Threading;
namespace FFMEGE {
public class Timer : IDisposable {
private System.Threading.Timer timer;
private DateTime _lastElapsedTime;
private DateTime lastNextDelayTime;
private long _interval;
private long _nextDelay;
private bool _enabled;
private bool _autoReset;
/// <summary>
/// Event that gets raised whenever an unhandled exception occurs within the Elapsed event.
/// </summary>
public static event UnhandledExceptionEventHandler UnhandledException;
/// <summary>
/// Creates a new Timer.
/// </summary>
public Timer() {
_autoReset = true;
timer = new System.Threading.Timer(new TimerCallback(timerCallback));
}
/// <summary>
/// Creates a new Timer.
/// </summary>
/// <param name="interval">Values for Interval and NextElapsedDelay, in Milliseconds.</param>
public Timer(long interval) : this() {
if (interval < 0L)
throw new ArgumentException("Argument 'interval' must be greater than 0", "interval");
_nextDelay = interval;
_interval = interval;
}
/// <summary>
/// Releases all resouces used by this instance of CreativeSpace.Util.Timer.
/// </summary>
public void Dispose() {
Dispose(true);
}
/// <summary>
/// Releases all resouces used by this instance of CreativeSpace.Util.Timer. For internal / CLR use only.
/// </summary>
protected void Dispose(bool disposing) {
if (disposing) {
timer.Dispose();
}
}
/// <summary>
/// If true, the elaspsed event will be raised each interval. If false the event will be raised only once. Defaults to true.
/// </summary>
public bool AutoReset {
get {
return _autoReset;
}
set {
if (_autoReset != value) {
_autoReset = value;
if (_enabled)
ApplyChange(true);
}
}
}
/// <summary>
/// Setting to true starts the timer. Setting to false stops it.
/// </summary>
public bool Enabled {
get {
return _enabled;
}
set {
if (_enabled != value) {
_enabled = value;
ApplyChange(false);
if (_enabled) {
_lastElapsedTime = DateTime.UtcNow;
}
}
}
}
/// <summary>
/// The time until the timer fires next, in Milliseconds, if Enabled is true.
/// </summary>
public long NextElapsedDelay {
get {
if (_enabled) {
return _nextDelay - (long)DateTime.UtcNow.Subtract(lastNextDelayTime).TotalMilliseconds;
}
return _nextDelay;
}
set {
if (_nextDelay != value) {
if (value < 0)
throw new ArgumentOutOfRangeException("NextElapsedDelay", "NextElapsedDelay must not be less than 0.");
_nextDelay = value;
if (_enabled) {
ApplyChange(false);
}
}
}
}
/// <summary>
/// The time between timer fires, in Milliseconds.
/// </summary>
public long Interval {
get {
return _interval;
}
set {
if (_interval != value) {
if (value <= 0)
throw new ArgumentOutOfRangeException("Interval", "Interval must be greater than 0.");
_interval = value;
if (_enabled)
ApplyChange(true);
}
}
}
/// <summary>
/// The last time the timer elapsed, in UTC time.
/// </summary>
public DateTime LastElapsedTimeUtc {
get {
return _lastElapsedTime;
}
}
private void ApplyChange(bool recalcNextDelay) {
long tempNextDelay = _nextDelay;
if (recalcNextDelay) {
tempNextDelay = tempNextDelay - (long)DateTime.UtcNow.Subtract(_lastElapsedTime).TotalMilliseconds;
}
else {
lastNextDelayTime = DateTime.UtcNow;
}
if (tempNextDelay < 0)
tempNextDelay = 0;
if (_enabled) {
if (_autoReset)
timer.Change(tempNextDelay, _interval);
else
timer.Change(tempNextDelay, Timeout.Infinite);
}
else {
timer.Change(Timeout.Infinite, Timeout.Infinite);
}
}
/// <summary>
/// Event that gets raised every time the timer elapses.
/// </summary>
public event ElapsedEventHandler Elapsed;
private void timerCallback(object obj) {
_nextDelay = _interval;
if (!_autoReset)
_enabled = false;
_lastElapsedTime = DateTime.UtcNow;
lastNextDelayTime = _lastElapsedTime;
ElapsedEventHandler temp = Elapsed;
if (temp != null) {
if (UnhandledException == null) {
temp(this, new ElapsedEventArgs(_lastElapsedTime));
}
else {
try {
temp(this, new ElapsedEventArgs(_lastElapsedTime));
}
catch (Exception ex) {
UnhandledException(this, new UnhandledExceptionEventArgs(ex, false));
}
}
}
}
}
/// <summary>
/// Delegate for the Timer.Elapsed event.
/// </summary>
/// <param name="sender">The timer that raises the event.</param>
/// <param name="e">The data for the event.</param>
public delegate void ElapsedEventHandler(object sender, ElapsedEventArgs e);
/// <summary>
/// Represents data for the Timer.Elapsed event.
/// </summary>
public class ElapsedEventArgs : EventArgs {
/// <summary>
/// Create a new ElapsedEventArgs
/// </summary>
/// <param name="elapsedTime">A DateTime representing the time the Timer elapsed, in UTC time.</param>
internal ElapsedEventArgs(DateTime elapsedTime) {
_elapsedTime = elapsedTime;
}
private DateTime _elapsedTime;
/// <summary>
/// Returns the UTC time at which the Timer elapsed
/// </summary>
public DateTime ElapsedTime {
get {
return _elapsedTime;
}
}
}
}