forked from JBontes/Life32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThdTim.pas
205 lines (175 loc) · 4.79 KB
/
ThdTim.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
unit ThdTim;
interface
uses
Windows, ExtCtrls, Messages, SysUtils, Classes;
{$WARN SYMBOL_PLATFORM OFF}
{$T+}
type
TThreadedTimer = class;
TTimerThread = class(TThread)
private
FOnFastTerminate: TNotifyEvent;
FTimer: pointer;
FMainForm: HWnd;
public
OwnerTimer: TThreadedTimer;
constructor Create(CreateSuspended: Boolean; FastTerminateEvent: TNotifyEvent; MainForm: HWnd; Timer: pointer);
destructor Destroy; override;
procedure Execute; override;
procedure DoTimer;
property OnFastTerminate: TNotifyEvent read FOnFastTerminate write FOnFastTerminate;
end;
// TTimerFields = class(TComponent)
// private
// FInterval: Cardinal;
// FWindowHandle: HWND;
// FOnTimer: TNotifyEvent;
// FEnabled: Boolean;
// end;
TThreadedTimer = class(TComponent)
private class var
FLastTimerId: integer;
private
FEnabled: Boolean;
FInterval: Integer;
FNewInterval: Integer;
FTimerThread: TTimerThread;
FThreadPriority: TThreadPriority;
FSynchronized: boolean;
FLowResTimer: TTimer;
function GetOnTimer: TNotifyEvent;
protected
procedure UpdateTimer;
procedure SetEnabled(Value: Boolean);
procedure SetInterval(Value: Integer);
procedure SetOnTimer(Value: TNotifyEvent);
procedure SetThreadPriority(Value: TThreadPriority);
procedure ThreadDone(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Wait;
procedure Proceed;
published
property Enabled: Boolean read FEnabled write SetEnabled default True;
property Interval: Integer read FInterval write SetInterval default 1000;
property OnTimer: TNotifyEvent read GetOnTimer write SetOnTimer;
property ThreadPriority: TThreadPriority read FThreadPriority write SetThreadPriority;
property Synchronized: boolean read FSynchronized write FSynchronized;
end;
procedure Register;
implementation
uses Forms;
constructor TTimerThread.Create(CreateSuspended: Boolean;FastTerminateEvent: TNotifyEvent; MainForm: HWnd; Timer: pointer);
begin
inherited Create(true);
FOnFastTerminate:= FastTerminateEvent;
FTimer:= Timer;
FMainForm:= MainForm;
if not(CreateSuspended) then Start;
end;
destructor TTimerThread.Destroy;
begin
inherited Destroy;
end;
procedure TTimerThread.Execute;
begin
while not(Terminated) do begin
if (OwnerTimer.FInterval > 0) then begin
Sleep(OwnerTimer.FInterval);
if not(Terminated) then DoTimer;
end else DoTimer;
end;
if Assigned(FOnFastTerminate) then FOnFastTerminate(Self);
end;
procedure TTimerThread.DoTimer;
begin
SendMessage(FMainForm, WM_Timer, NativeInt(FTimer), 0);
end;
constructor TThreadedTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Inc(FLastTimerId);
FEnabled := false;
FInterval := 1000;
FThreadPriority := tpNormal;
FTimerThread := nil;
FLowResTimer:= TTimer.Create(AOwner);
FLowResTimer.Enabled:= false;
end;
destructor TThreadedTimer.Destroy;
begin
FEnabled := False;
UpdateTimer;
FTimerThread.Free;
FLowResTimer.Free;
inherited Destroy;
end;
function TThreadedTimer.GetOnTimer: TNotifyEvent;
begin
Result:= FLowResTimer.OnTimer;
end;
procedure TThreadedTimer.Proceed;
begin
Enabled:= true;
end;
procedure TThreadedTimer.UpdateTimer;
begin
if (FNewInterval > 100) then begin
if Assigned(FTimerThread) then FTimerThread.Terminate;
FLowResTimer.Interval:= FNewInterval;
FLowResTimer.Enabled:= Self.Enabled;
FInterval:= FNewInterval;
end else begin
FLowResTimer.Enabled:= false;
FInterval:= FNewInterval;
if FTimerThread = nil then begin
Assert(Owner is TForm);
FTimerThread:= TTimerThread.Create(true, ThreadDone, TForm(Owner).Handle, Self);
end;
FTimerThread.OwnerTimer:= self;
FTimerThread.Priority:= FThreadPriority;
if (FInterval >= 0) and FEnabled and Assigned(FLowResTimer.OnTimer) then begin
if FTimerThread.Suspended then FTimerThread.Start;
end;
end;
end;
procedure TThreadedTimer.Wait;
begin
Enabled:= false;
end;
procedure TThreadedTimer.SetEnabled(Value: Boolean);
begin
if Value <> FEnabled then begin
FEnabled := Value;
UpdateTimer;
end;
end;
procedure TThreadedTimer.SetInterval(Value: Integer);
begin
if Value <> FInterval then begin
FNewInterval := Value;
UpdateTimer;
end;
end;
procedure TThreadedTimer.SetOnTimer(Value: TNotifyEvent);
begin
FLowResTimer.OnTimer:= Value;
UpdateTimer;
end;
procedure TThreadedTimer.SetThreadPriority(Value: TThreadPriority);
begin
if Value <> FThreadPriority then begin
FThreadPriority := Value;
UpdateTimer;
end;
end;
procedure TThreadedTimer.ThreadDone(Sender: TObject);
begin
if (Sender = FTimerThread) then FTimerThread:= nil;
end;
procedure Register;
begin
RegisterComponents('Johan', [TThreadedTimer]);
end;
end.