-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcatimer.pas
212 lines (182 loc) · 5.32 KB
/
catimer.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
unit caTimer;
{$INCLUDE ca.inc}
interface
uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ExtCtrls,
MMSystem,
caTypes;
type
TcaTimer = class;
//----------------------------------------------------------------------------
// TcaTimerThread
//----------------------------------------------------------------------------
TcaTimerThread = class(TThread)
private
FTimer: TcaTimer;
protected
procedure DoExecute;
public
constructor CreateTimerThread(Timer: TcaTimer);
procedure Execute; override;
end;
//----------------------------------------------------------------------------
// IcaTimer
//----------------------------------------------------------------------------
IcaTimer = interface
['{0BB08B16-3CDF-4211-BC6E-D08B563F9F09}']
function GetEnabled: Boolean;
function GetInterval: LongWord;
function GetOnTimer: TNotifyEvent;
function GetPriority: TThreadPriority;
procedure SetEnabled(const Value: Boolean);
procedure SetInterval(const Value: LongWord);
procedure SetOnTimer(const Value: TNotifyEvent);
procedure SetPriority(const Value: TThreadPriority);
procedure Start;
procedure Stop;
property Enabled: Boolean read GetEnabled write SetEnabled;
property Interval: LongWord read GetInterval write SetInterval;
property OnTimer: TNotifyEvent read GetOnTimer write SetOnTimer;
property Priority: TThreadPriority read GetPriority write SetPriority;
end;
//----------------------------------------------------------------------------
// TcaTimer
//----------------------------------------------------------------------------
TcaTimer = class(TComponent, IcaTimer)
private
FInterval: LongWord;
FPriority: TThreadPriority;
FOnTimer: TNotifyEvent;
FContinue: Boolean;
FRunning: Boolean;
FEnabled: Boolean;
function GetEnabled: Boolean;
function GetInterval: LongWord;
function GetOnTimer: TNotifyEvent;
function GetPriority: TThreadPriority;
procedure SetEnabled(const Value: Boolean);
procedure SetInterval(const Value: LongWord);
procedure SetOnTimer(const Value: TNotifyEvent);
procedure SetPriority(const Value: TThreadPriority);
protected
property Continue: Boolean read FContinue write FContinue;
public
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
procedure Start;
procedure Stop;
published
property Enabled: Boolean read GetEnabled write SetEnabled;
property Interval: LongWord read GetInterval write SetInterval;
property OnTimer: TNotifyEvent read GetOnTimer write SetOnTimer;
property Priority: TThreadPriority read GetPriority write SetPriority;
end;
implementation
//----------------------------------------------------------------------------
// TcaTimerThread
//----------------------------------------------------------------------------
constructor TcaTimerThread.CreateTimerThread(Timer: TcaTimer);
begin
inherited Create(True);
FTimer := Timer;
FreeOnTerminate := True;
end;
procedure TcaTimerThread.Execute;
var
SleepTime, Last: LongWord;
begin
while FTimer.Continue do
begin
Last := timeGetTime;
Synchronize(DoExecute);
SleepTime := FTimer.Interval - (timeGetTime - Last);
if SleepTime < 10 then SleepTime := 10;
Sleep(SleepTime);
end;
end;
procedure TcaTimerThread.DoExecute;
begin
if Assigned(FTimer.OnTimer) then FTimer.OnTimer(FTimer);
end;
//----------------------------------------------------------------------------
// TcaTimer
//----------------------------------------------------------------------------
constructor TcaTimer.Create(Owner: TComponent);
begin
inherited;
FPriority := tpNormal;
end;
destructor TcaTimer.Destroy;
begin
Stop;
inherited;
end;
function TcaTimer.GetEnabled: Boolean;
begin
Result := FEnabled;
end;
function TcaTimer.GetInterval: LongWord;
begin
Result := FInterval;
end;
function TcaTimer.GetOnTimer: TNotifyEvent;
begin
Result := FOnTimer;
end;
function TcaTimer.GetPriority: TThreadPriority;
begin
Result := FPriority;
end;
procedure TcaTimer.SetEnabled(const Value: Boolean);
begin
if Value <> FEnabled then
begin
FEnabled := Value;
if FEnabled then
Start
else
Stop;
end;
end;
procedure TcaTimer.SetInterval(const Value: LongWord);
begin
FInterval := Value;
end;
procedure TcaTimer.SetOnTimer(const Value: TNotifyEvent);
begin
FOnTimer := Value;
end;
procedure TcaTimer.SetPriority(const Value: TThreadPriority);
begin
FPriority := Value;
end;
procedure TcaTimer.Start;
var
TimerThread: TcaTimerThread;
begin
if not FRunning then
begin
FContinue := True;
if not (csDesigning in ComponentState) then
begin
TimerThread := TcaTimerThread.CreateTimerThread(Self);
TimerThread.Priority := FPriority;
TimerThread.Resume;
end;
FRunning := True;
end;
end;
procedure TcaTimer.Stop;
begin
FContinue := False;
FRunning := False;
end;
end.