This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreadCustom.pas
265 lines (226 loc) · 5.15 KB
/
ThreadCustom.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
unit ThreadCustom;
interface
uses
System.Classes, System.SysUtils, System.Threading, System.SyncObjs;
type
Exception = System.SysUtils.Exception;
TOnError = reference to procedure(const E: Exception; out AContinue: Boolean);
THC = class(TThread)
private
FEvent: TEvent;
FInitialize: TThreadProcedure;
FFinish: TThreadProcedure;
FError: TOnError;
FAdd: TArray<ITask>;
protected
procedure Execute; override;
public
function Initialize(const AProc: TThreadProcedure): THC;
function InitializeAsync(const AProc: TThreadProcedure): THC;
function InitializeSync(const AProc: TThreadProcedure): THC;
function Add(const AProc: TThreadProcedure): THC;
function AddAsync(const AProc: TThreadProcedure): THC;
function AddSync(const AProc: TThreadProcedure): THC;
function Error(const AProc: TOnError): THC;
function Finish(const AProc: TThreadProcedure): THC;
function FinishAsync(const AProc: TThreadProcedure): THC;
function FinishSync(const AProc: TThreadProcedure): THC;
function Start: THC;
function StartWait: THC;
procedure Finalize(var AObject);
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
class function New: THC;
class procedure Synchronize(const AProc: TThreadProcedure);
class procedure SynchronizeAsync(const AProc: TThreadProcedure);
end;
implementation
{ THC }
class function THC.New: THC;
begin
Result := THC.Create(True);
end;
class procedure THC.Synchronize(const AProc: TThreadProcedure);
begin
if Assigned(AProc) then
if (Current.ThreadID = MainThreadID) then
AProc
else
TThread.Synchronize(nil, AProc);
end;
class procedure THC.SynchronizeAsync(const AProc: TThreadProcedure);
begin
if Assigned(AProc) then
if Current.ThreadID = MainThreadID then
AProc
else
TThread.Queue(nil, AProc);
end;
procedure THC.AfterConstruction;
begin
FEvent := TEvent.Create;
FAdd := [];
FInitialize := nil;
FFinish := nil;
FError := nil;
FreeOnTerminate := True;
inherited Start;
end;
procedure THC.BeforeDestruction;
begin
FEvent.DisposeOf;
end;
function THC.Initialize(const AProc: TThreadProcedure): THC;
begin
Result := Self;
FInitialize := AProc;
end;
function THC.InitializeAsync(const AProc: TThreadProcedure): THC;
begin
Result := Initialize(
procedure
begin
Self.SynchronizeAsync(AProc);
end);
end;
function THC.InitializeSync(const AProc: TThreadProcedure): THC;
begin
Result := Initialize(
procedure
begin
Self.Synchronize(AProc);
end);
end;
function THC.Add(const AProc: TThreadProcedure): THC;
begin
Result := Self;
FAdd := Concat(FAdd, [TTask.Create(
procedure
begin
if not Terminated then
AProc;
end)]);
end;
function THC.AddAsync(const AProc: TThreadProcedure): THC;
begin
Result := Add(
procedure
begin
Self.SynchronizeAsync(AProc);
end);
end;
function THC.AddSync(const AProc: TThreadProcedure): THC;
begin
Result := Add(
procedure
begin
Self.Synchronize(AProc);
end);
end;
function THC.Error(const AProc: TOnError): THC;
begin
Result := Self;
FError := AProc;
end;
function THC.Finish(const AProc: TThreadProcedure): THC;
begin
Result := Self;
FFinish :=
procedure
begin
if not Terminated then
AProc;
end;
end;
function THC.FinishAsync(const AProc: TThreadProcedure): THC;
begin
Result := Finish(
procedure
begin
Self.SynchronizeAsync(AProc);
end);
end;
function THC.FinishSync(const AProc: TThreadProcedure): THC;
begin
Result := Finish(
procedure
begin
Self.Synchronize(AProc);
end);
end;
function THC.Start: THC;
begin
Result := Self;
FEvent.SetEvent;
end;
function THC.StartWait: THC;
begin
FreeOnTerminate := False;
Result := Start;
WaitFor;
DisposeOf;
end;
procedure THC.Execute;
var
LIsContinue: Boolean;
begin
FEvent.WaitFor(INFINITE);
FEvent.ResetEvent;
LIsContinue := True;
try
try
if Assigned(FInitialize) then
FInitialize;
if Length(FAdd) > 0 then
begin
TParallel.For(Low(FAdd), High(FAdd),
procedure(Index: Integer)
begin
FAdd[Index].Start;
end);
try
TTask.WaitForAll(FAdd);
except
on E: EAggregateException do
if Assigned(FError) then
FError(E.InnerExceptions[0], LIsContinue)
else
raise;
on E: Exception do
Exit;
end;
end;
except
on E: Exception do
if Assigned(FError) then
FError(E, LIsContinue)
else
raise;
end;
finally
try
if Assigned(FFinish) and LIsContinue then
FFinish;
finally
if not FreeOnTerminate then
Terminate;
end;
end;
end;
procedure THC.Finalize(var AObject);
begin
if not Assigned(TObject(AObject)) then
Exit;
if not Terminated then
begin
Terminate;
if Length(FAdd) > 0 then
TParallel.For(Low(FAdd), High(FAdd),
procedure(Index: Integer)
begin
FAdd[Index].Cancel;
end);
Pointer(AObject) := nil;
end;
end;
end.