forked from dim-s/soulengine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimpleTimer.pas
298 lines (247 loc) · 7.22 KB
/
SimpleTimer.pas
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
{*****************************************************************}
{ SimpleTimer is a timer class. It has the same timer resolution }
{ as TTimer, but it is more lightweight because it's derived from }
{ TObject in stead of TComponent. Furthermore, the same handle is }
{ shared between multiple instances of SimpleTimer. }
{ This makes it ideal for developers who need a timer in their }
{ own components or applications, but want to keep the resource }
{ usage minimal. }
{ }
{ The unit is freeware. Feel free to use and improve it. }
{ I would be pleased to hear what you think. }
{ }
{ Troels Jakobsen - [email protected] }
{ Copyright (c) 2006 }
{*****************************************************************}
unit SimpleTimer;
{ Some methods have moved to the Classes unit in D6 and are thus deprecated.
Using the following compiler directives we handle that situation. }
{$DEFINE DELPHI_6_UP}
{$IFDEF VER80} {$UNDEF DELPHI_6_UP} {$ENDIF} // D1
{$IFDEF VER90} {$UNDEF DELPHI_6_UP} {$ENDIF} // D2
{$IFDEF VER100} {$UNDEF DELPHI_6_UP} {$ENDIF} // D3
{$IFDEF VER120} {$UNDEF DELPHI_6_UP} {$ENDIF} // D4
{$IFDEF VER130} {$UNDEF DELPHI_6_UP} {$ENDIF} // D5
{$IFDEF VER93} {$UNDEF DELPHI_6_UP} {$ENDIF} // BCB1
{$IFDEF VER110} {$UNDEF DELPHI_6_UP} {$ENDIF} // BCB3
{$IFDEF VER125} {$UNDEF DELPHI_6_UP} {$ENDIF} // BCB4
{$IFDEF VER135} {$UNDEF DELPHI_6_UP} {$ENDIF} // BCB5
interface
uses
Windows, Classes;
type
TSimpleTimer = class(TObject)
private
FId: UINT;
FEnabled: Boolean;
FInterval: Cardinal;
FAutoDisable: Boolean;
FTag: Integer;
FOnTimer: TNotifyEvent;
procedure SetEnabled(Value: Boolean);
procedure SetInterval(Value: Cardinal);
procedure SetOnTimer(Value: TNotifyEvent);
procedure Initialize(AInterval: Cardinal; AOnTimer: TNotifyEvent);
protected
function Start: Boolean;
function Stop(Disable: Boolean): Boolean;
public
constructor Create;
constructor CreateEx(AInterval: Cardinal; AOnTimer: TNotifyEvent);
destructor Destroy; override;
property Enabled: Boolean read FEnabled write SetEnabled;
property Interval: Cardinal read FInterval write SetInterval default 1000;
property AutoDisable: Boolean read FAutoDisable write FAutoDisable;
property Tag: Integer read Ftag write Ftag default 0;
property OnTimer: TNotifyEvent read FOnTimer write SetOnTimer;
end;
function GetSimpleTimerCount: Cardinal;
function GetSimpleTimerActiveCount: Cardinal;
implementation
uses
Messages{$IFNDEF DELPHI_6_UP}, Forms {$ENDIF};
type
TSimpleTimerHandler = class(TObject)
private
RefCount: Cardinal;
ActiveCount: Cardinal;
FWindowHandle: HWND;
procedure WndProc(var Msg: TMessage);
public
constructor Create;
destructor Destroy; override;
procedure AddTimer;
procedure RemoveTimer;
end;
var
SimpleTimerHandler: TSimpleTimerHandler = nil;
function GetSimpleTimerCount: Cardinal;
begin
if Assigned(SimpleTimerHandler) then
Result := SimpleTimerHandler.RefCount
else
Result := 0;
end;
function GetSimpleTimerActiveCount: Cardinal;
begin
if Assigned(SimpleTimerHandler) then
Result := SimpleTimerHandler.ActiveCount
else
Result := 0;
end;
{--------------- TSimpleTimerHandler ------------------}
constructor TSimpleTimerHandler.Create;
begin
inherited Create;
{$IFDEF DELPHI_6_UP}
FWindowHandle := Classes.AllocateHWnd(WndProc);
{$ELSE}
FWindowHandle := AllocateHWnd(WndProc);
{$ENDIF}
end;
destructor TSimpleTimerHandler.Destroy;
begin
{$IFDEF DELPHI_6_UP}
Classes.DeallocateHWnd(FWindowHandle);
{$ELSE}
DeallocateHWnd(FWindowHandle);
{$ENDIF}
inherited Destroy;
end;
procedure TSimpleTimerHandler.AddTimer;
begin
Inc(RefCount);
end;
procedure TSimpleTimerHandler.RemoveTimer;
begin
if RefCount > 0 then
Dec(RefCount);
end;
procedure TSimpleTimerHandler.WndProc(var Msg: TMessage);
var
Timer: TSimpleTimer;
begin
if Msg.Msg = WM_TIMER then
begin
{$WARNINGS OFF}
Timer := TSimpleTimer(Msg.wParam);
{$WARNINGS ON}
if Timer.FAutoDisable then
Timer.Stop(True);
// Call OnTimer event method if assigned
if Assigned(Timer.FOnTimer) then
Timer.FOnTimer(Timer);
end
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;
{---------------- Container management ----------------}
procedure AddTimer;
begin
if not Assigned(SimpleTimerHandler) then
// Create new handler
SimpleTimerHandler := TSimpleTimerHandler.Create;
SimpleTimerHandler.AddTimer;
end;
procedure RemoveTimer;
begin
if Assigned(SimpleTimerHandler) then
begin
SimpleTimerHandler.RemoveTimer;
if SimpleTimerHandler.RefCount = 0 then
begin
// Destroy handler
SimpleTimerHandler.Free;
SimpleTimerHandler := nil;
end;
end;
end;
{------------------- TSimpleTimer ---------------------}
constructor TSimpleTimer.Create;
begin
inherited Create;
Initialize(1000, nil);
end;
constructor TSimpleTimer.CreateEx(AInterval: Cardinal; AOnTimer: TNotifyEvent);
begin
inherited Create;
Initialize(AInterval, AOnTimer);
end;
destructor TSimpleTimer.Destroy;
begin
if FEnabled then
Stop(True);
RemoveTimer; // Container management
inherited Destroy;
end;
procedure TSimpleTimer.Initialize(AInterval: Cardinal; AOnTimer: TNotifyEvent);
begin
{$WARNINGS OFF}
FId := UINT(Self); // Use Self as id in call to SetTimer and callback method
{$WARNINGS ON}
FAutoDisable := False;
FEnabled := False;
FInterval := AInterval;
SetOnTimer(AOnTimer);
AddTimer; // Container management
end;
procedure TSimpleTimer.SetEnabled(Value: Boolean);
begin
if Value then
Start
else
Stop(True);
end;
procedure TSimpleTimer.SetInterval(Value: Cardinal);
begin
if Value <> FInterval then
begin
FInterval := Value;
if FEnabled then
if FInterval <> 0 then
Start
else
Stop(False);
end;
end;
procedure TSimpleTimer.SetOnTimer(Value: TNotifyEvent);
begin
FOnTimer := Value;
if (not Assigned(Value)) and (FEnabled) then
Stop(False);
end;
function TSimpleTimer.Start: Boolean;
begin
if FInterval = 0 then
begin
Result := False;
Exit;
end;
if FEnabled then
Stop(True);
// Result := (SetTimer(SimpleTimerHandler.FWindowHandle, FId, FInterval, @TimerProc) <> 0);
Result := (SetTimer(SimpleTimerHandler.FWindowHandle, FId, FInterval, nil) <> 0);
if Result then
begin
FEnabled := True;
Inc(SimpleTimerHandler.ActiveCount);
end
{ else
raise EOutOfResources.Create(SNoTimers); }
end;
function TSimpleTimer.Stop(Disable: Boolean): Boolean;
begin
if Disable then
FEnabled := False;
Result := KillTimer(SimpleTimerHandler.FWindowHandle, FId);
if Result and (SimpleTimerHandler.ActiveCount > 0) then
Dec(SimpleTimerHandler.ActiveCount);
end;
initialization
finalization
if Assigned(SimpleTimerHandler) then
begin
SimpleTimerHandler.Free;
SimpleTimerHandler := nil;
end;
end.