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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Timer;
interface
uses
Boolean,
Event,
Integer._32.Positive,
Object_.Base,
Thread.Synchronization.WaitableQueue,
Windows.Api.Type_;
type
TTimer = class(TObject)
protected
FOwner: TBaseObject;
FKilled: TBoolean;
FScheduled: TBoolean;
FDelay_Milliseconds: TPositiveInteger32;
procedure ApiSchedule(); virtual; abstract;
procedure ApiKill(); virtual; abstract;
public
constructor Create(); virtual;
destructor Destroy(); override;
procedure Schedule(Owner: TBaseObject);
procedure Kill();
procedure Pause();
procedure Resume();
property Scheduled: TBoolean read FScheduled;
property Killed: TBoolean read FKilled;
property Delay_Milliseconds: TPositiveInteger32 read FDelay_Milliseconds write FDelay_Milliseconds;
end;
// Not very exact but useful timer
// Timer events occur synchronized with the application main message loop which prevents synchronization problems
TLowResolutionSynchronousTimer = class(TTimer)
protected
procedure ApiSchedule(); override;
procedure ApiKill(); override;
procedure HandleFired();
protected
class procedure CallBack(Wnd: THWND; Msg: UINT; TimerID: UINT_PTR; SysTime: DWORD); stdcall; static;
end;
// Very exact timer
// Timer events occur asynchronously from the application main message loop, synchronization problems are possible
THighResolutionAsynchronousTimer = class(TTimer)
protected
Handle: TPositiveInteger32;
procedure ApiSchedule(); override;
procedure ApiKill(); override;
procedure HandleFired(); virtual;
protected
class procedure CallBack(wTimerID, msg: UINT; dwUser, dw1, dw2: DWORD); static; stdcall;
end;
TTimerFired = class(TEvent)
public
Timer: TTimer;
constructor Create(Timer: TTimer); virtual;
function GetSender(): TObject; override;
end;
TEventQueueTarget = class(TBaseObject)
public
EventQueue: TWaitableQueue<TEvent>;
constructor Create(EventQueue: TWaitableQueue<TEvent>); reintroduce; virtual;
function HandleEvent(Event: TEvent): TBoolean; override;
end;
TEventQueueTimer = class(TObject)
private
function GetDelay_Milliseconds(): TPositiveInteger32;
procedure SetDelay_Milliseconds(const Value: TPositiveInteger32);
protected
Target: TEventQueueTarget;
Timer: THighResolutionAsynchronousTimer;
public
constructor Create(Queue: TWaitableQueue<TEvent>); virtual;
destructor Destroy(); override;
procedure Schedule();
property Delay_Milliseconds: TPositiveInteger32 read GetDelay_Milliseconds write SetDelay_Milliseconds;
end;
implementation
uses
Application,
Exception,
Windows.Api.Error,
Windows.Api.MultiMedia.Timer,
Windows.Api.UserInterface.Timer;
{ THighResolutionAsynchronousTimer }
procedure THighResolutionAsynchronousTimer.ApiKill();
begin
timeKillEvent(Handle);
end;
procedure THighResolutionAsynchronousTimer.ApiSchedule();
begin
Handle := timeSetEvent(Delay_Milliseconds, 100, @THighResolutionAsynchronousTimer.CallBack, TPositiveInteger32(Self), TIME_ONESHOT);
end;
class procedure THighResolutionAsynchronousTimer.CallBack(wTimerID, msg: UINT; dwUser, dw1, dw2: DWORD);
begin
THighResolutionAsynchronousTimer(dwUser).HandleFired();
end;
procedure THighResolutionAsynchronousTimer.HandleFired();
var
Event: TTimerFired;
begin
FScheduled := False;
Event := TTimerFired.Create(Self);
FOwner.HandleEvent(Event);
Event.Free();
end;
{ TLowResolutionSynchronousTimer }
procedure TLowResolutionSynchronousTimer.ApiKill();
begin
KillTimer(TApplication.Instance.MainWindow.Handle, UINT_PTR(Self));
end;
procedure TLowResolutionSynchronousTimer.ApiSchedule();
begin
if SetTimer(TApplication.Instance.MainWindow.Handle, UINT_PTR(Self), Delay_Milliseconds, @TLowResolutionSynchronousTimer.CallBack) = 0 then
EWindows32.RaiseLast();
end;
class procedure TLowResolutionSynchronousTimer.CallBack(Wnd: THWND; Msg: UINT; TimerID: UINT_PTR; SysTime: DWORD);
begin
TLowResolutionSynchronousTimer(TimerID).HandleFired();
end;
procedure TLowResolutionSynchronousTimer.HandleFired();
var
Event: TTimerFired;
begin
ApiKill();
FScheduled := False;
Event := TTimerFired.Create(Self);
FOwner.HandleEvent(Event);
Event.Free();
end;
{ TTimer }
constructor TTimer.Create();
begin
inherited Create();
Delay_Milliseconds := 1000;
FScheduled := False;
end;
destructor TTimer.Destroy();
begin
if Scheduled then
Kill();
inherited Destroy();
end;
procedure TTimer.Kill();
begin
FKilled := True;
if Scheduled then
ApiKill();
FScheduled := False;
end;
procedure TTimer.Pause();
begin
if Scheduled then
ApiKill();
end;
procedure TTimer.Resume();
begin
if Scheduled then
ApiSchedule();
end;
procedure TTimer.Schedule(Owner: TBaseObject);
begin
if not Scheduled then
begin
FOwner := Owner;
FScheduled := True;
FKilled := False;
ApiSchedule();
end
else
raise EException.Create('Timer already scheduled!');
end;
{ TEventQueueTarget }
constructor TEventQueueTarget.Create(EventQueue: TWaitableQueue<TEvent>);
begin
inherited Create();
Self.EventQueue := EventQueue;
end;
function TEventQueueTarget.HandleEvent(Event: TEvent): TBoolean;
begin
if Event is TTimerFired then
begin
Result := True;
EventQueue.Queue(TTimerFired.Create((Event as TTimerFired).Timer));
end
else
Result := inherited HandleEvent(Event);
end;
{ TTimerFired }
constructor TTimerFired.Create(Timer: TTimer);
begin
inherited Create();
Self.Timer := Timer;
end;
function TTimerFired.GetSender(): TObject;
begin
Result := Timer;
end;
{ TEventQueueTimer }
constructor TEventQueueTimer.Create(Queue: TWaitableQueue<TEvent>);
begin
inherited Create();
Target := TEventQueueTarget.Create(Queue);
Timer := THighResolutionAsynchronousTimer.Create();
end;
destructor TEventQueueTimer.Destroy();
begin
Timer.Free();
Target.Free();
inherited Destroy();
end;
function TEventQueueTimer.GetDelay_Milliseconds(): TPositiveInteger32;
begin
Result := Timer.Delay_Milliseconds;
end;
procedure TEventQueueTimer.Schedule();
begin
Timer.Schedule(Target);
end;
procedure TEventQueueTimer.SetDelay_Milliseconds(const Value: TPositiveInteger32);
begin
Timer.Delay_Milliseconds := Value;
end;
end.
|