-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallel.pas
More file actions
executable file
·61 lines (46 loc) · 1.22 KB
/
Parallel.pas
File metadata and controls
executable file
·61 lines (46 loc) · 1.22 KB
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
unit Parallel;
interface
uses Tasks,Classes,SyncObjs,SysUtils;
type
TForEachAction=procedure (i:integer;o:TObject);
TTaskParameter=class
proc:TForEachAction;
index:integer;
o:TObject;
task:ITask;
end;
procedure ForEach(l:TList;proc:TForEachAction;callback:TTaskProc); overload;
procedure ForEach(l:TList;proc:TForEachAction); overload;
implementation
//------------------------------------------------------------------------------
procedure CallForEachAction(o:TObject);
var
p:TTaskParameter;
begin
p:=o as TTaskParameter;
p.proc(p.index,p.o);
end;
//------------------------------------------------------------------------------
procedure ForEach(l:TList;proc:TForEachAction;callback:TTaskProc);
var
i:integer;
t:ITask;
p:TTaskParameter;
begin
if((l=nil)or(l.Count=0))then
exit;
for i:=0 to l.Count-1 do
begin
p:=TTaskParameter.Create;
p.proc:=proc;
p.index:=i;
p.o:=l[i];
t:=TTask.Run(@CallForEachAction,p).ContinueWith(@callback,p,[coOnlyOnSuccess,coOnMainThread]);
p.task:=t;
end;
end;
procedure ForEach(l:TList;proc:TForEachAction);
begin
ForEach(l,proc,nil);
end;
end.