-
Notifications
You must be signed in to change notification settings - Fork 3
/
ThreadTimer.pas
201 lines (172 loc) · 4.04 KB
/
ThreadTimer.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
unit ThreadTimer;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses
SysUtils,
Classes,
SyncObjs;
type
TThreadTimer = class;
TTimerThread = class(TThread)
private
FEvent: TEvent;
FRestartThread: boolean;
FInterval: longword;
FOwner: TThreadTimer;
procedure Cycle;
procedure DoTimer;
protected
procedure Execute; override;
public
constructor Create(AOwner: TThreadTimer); overload;
destructor Destroy; override;
property RestartThread: boolean Read FRestartThread Write FRestartThread;
property Interval: longword Read FInterval Write FInterval default 1000;
end;
TThreadTimer = class(TComponent)
private
FOnTimer: TNotifyEvent;
FInterval: cardinal;
FEnabled: boolean;
FTimerThread: TTimerThread;
procedure SetInterval(const Value: cardinal);
procedure SetEnabled(const Value: boolean);
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Start;
procedure Stop;
procedure Restart(EnableTimer: boolean = False);
procedure TermiteAndWait;
procedure Termite;
function Terminaed: boolean;
{ Public declarations }
published
property Interval: cardinal Read FInterval Write SetInterval default 1000;
property OnTimer: TNotifyEvent Read FOnTimer Write FOnTimer;
property Enabled: boolean Read FEnabled Write SetEnabled default False;
{ Published declarations }
end;
function CreateThreadTimer(AInterval: integer; AOnTimer: TNotifyEvent): TThreadTimer;
procedure Register;
implementation
function CreateThreadTimer(AInterval: integer; AOnTimer: TNotifyEvent): TThreadTimer;
begin
Result := TThreadTimer.Create(nil);
Result.Enabled := False;
Result.Interval := AInterval;
Result.OnTimer := AOnTimer;
end;
procedure Register;
begin
RegisterComponents('Makhaon', [TThreadTimer]);
end;
{ TThreadTimer }
constructor TThreadTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FInterval := 1000;
FEnabled := False;
FTimerThread := TTimerThread.Create(Self);
end;
destructor TThreadTimer.Destroy;
begin
FreeAndNil(FTimerThread);
inherited;
end;
procedure TThreadTimer.Restart(EnableTimer: boolean = False);
begin
if EnableTimer then
FEnabled := True;
if FEnabled then
FTimerThread.Interval := FInterval
else
FTimerThread.Interval := INFINITE;
FTimerThread.RestartThread := True;
FTimerThread.Cycle;
end;
procedure TThreadTimer.SetEnabled(const Value: boolean);
begin
if FEnabled <> Value then
begin
FEnabled := Value;
Restart;
end;
end;
procedure TThreadTimer.SetInterval(const Value: cardinal);
begin
FInterval := Value;
if FEnabled then
Restart;
end;
procedure TThreadTimer.Start;
begin
SetEnabled(True);
end;
procedure TThreadTimer.Stop;
begin
SetEnabled(False);
end;
function TThreadTimer.Terminaed: boolean;
begin
Result := not Assigned(FTimerThread) or FTimerThread.Terminated;
end;
procedure TThreadTimer.Termite;
begin
if Assigned(FTimerThread) then
with FTimerThread do
Terminate;
end;
procedure TThreadTimer.TermiteAndWait;
begin
if Assigned(FTimerThread) then
with FTimerThread do
begin
Terminate;
Cycle;
WaitFor;
end;
end;
{ TTimerThread }
constructor TTimerThread.Create(AOwner: TThreadTimer);
begin
FEvent := TEvent.Create(nil, True, False, '');
FOwner := AOwner;
FInterval := INFINITE;
inherited Create(False);
end;
destructor TTimerThread.Destroy;
begin
Terminate;
Cycle;
WaitFor;
FreeAndNil(FEvent);
inherited;
end;
procedure TTimerThread.DoTimer;
begin
if Assigned(FOwner.FOnTimer) then
FOwner.FOnTimer(Self);
end;
procedure TTimerThread.Execute;
begin
while not Terminated do
begin
FEvent.ResetEvent;
FEvent.WaitFor(FInterval);
if FRestartThread then
FRestartThread := False
else if not Terminated then
DoTimer;
end;
end;
procedure TTimerThread.Cycle;
begin
FEvent.SetEvent;
end;
end.