-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrm_timer.pas
152 lines (129 loc) · 2.75 KB
/
rm_timer.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
unit rm_timer;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, RadioModule, ExtCtrls, radiomessage;
type
PTimerNode = ^TTimerNode;
TTimerNode = record
Next: PTimerNode;
Id: PtrUInt;
Obj: TTimer;
end;
{ TRadioTimer }
TRadioTimer = class(TRadioModule)
private
FHead: TTimerNode;
procedure Lock;
procedure Unlock;
procedure OnTimer(Sender: TObject);
// *lock* required when call FindTimer
function FindTimer(const AId: PtrUInt): PTimerNode;
protected
procedure ProccessCustomMessage(const Msg: TRadioMessage; var Ret: Integer); override;
procedure CreateTimer(const AId: PtrUInt; const AInterval: Cardinal);
procedure DeleteTimer(const AId: PtrUInt);
procedure Describe(Strs: TStrings); override;
end;
implementation
uses
RadioSystem;
{ TRadioTimer }
procedure TRadioTimer.Lock;
begin
RadioGlobalLock
end;
procedure TRadioTimer.Unlock;
begin
RadioGlobalUnlock;
end;
procedure TRadioTimer.OnTimer(Sender: TObject);
var
P: PTimerNode;
T: TTimer;
M: TRadioMessage;
begin
T := Sender as TTimer;
P := PTimerNode(Pointer(T.Tag));
with M do
begin
Id := radiomessage.RM_TIMER;
Sender := Name;
ParamH := P^.Id and $FFFF;
ParamL := T.Interval;
end;
RadioPostMessage(M, Cardinal(P^.Id shr 32));
end;
function TRadioTimer.FindTimer(const AId: PtrUInt): PTimerNode;
begin
Result := FHead.Next;
while Assigned(Result) and (Result^.Id <> Id) do Result := Result^.Next;
end;
procedure TRadioTimer.ProccessCustomMessage(const Msg: TRadioMessage;
var Ret: Integer);
begin
case Msg.Id of
RM_CREATE_TIMER:
begin
CreateTimer(Msg.ParamH, Cardinal(Msg.ParamL));
GraphInvalidate;
end;
RM_DELETE_TIMER:
begin
DeleteTimer(Msg.ParamH);
GraphInvalidate;
end
else
inherited;
end;
end;
procedure TRadioTimer.CreateTimer(const AId: PtrUInt; const AInterval: Cardinal
);
label
Quit;
var
P: PTimerNode;
begin
if AInterval = 0 then Exit;
Lock;
P := FindTimer(Id);
if Assigned(P) then goto Quit;
New(P);
P^.Id := Id;
P^.Next := FHead.Next;
FHead.Next := P;
P^.Obj := TTimer.Create(nil);
with P^.Obj do
begin
Interval := AInterval;
OnTimer := @Self.OnTimer;
Tag := PtrInt(P);
Enabled := True;
end;
Quit:
Unlock;
end;
procedure TRadioTimer.DeleteTimer(const AId: PtrUInt);
var
P: PTimerNode;
begin
Lock;
P := FindTimer(Id);
if Assigned(P) then
begin
P^.Obj.Free;
FHead.Next := P^.Next;
Dispose(P);
end;
Unlock;
end;
procedure TRadioTimer.Describe(Strs: TStrings);
begin
if Assigned(FHead.Next) then
Strs.Add('^bTimers running')
else
Strs.Add('^bNo timer');
end;
initialization
RegisterModule(TRadioModuleClass(TRadioTimer.ClassType));
end.