-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitMerger.pas
67 lines (57 loc) · 1.82 KB
/
UnitMerger.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
unit UnitMerger;
interface
uses
Windows, SysUtils, Classes, SyncObjs,
Common, UnitThreadManager, UnitBlockList, UnitBlockMerger;
function MergeBlocks(const OnProgress: TProgressProc = nil): string;
implementation
procedure OnBlocksMerged(const ProtoThread: TProtoThread);
begin
// SetBlockFinished(ProtoThread);
end;
// Èñïîëüçîâàòü Thread.WaitFor íåëüçÿ, ò.ê. ó ïîòîêîâ óñòàíîâëåíî FreeOnTerminate è Free ïðîèñõîäèò áûñòðåå, ÷åì âûõîä è WaitFor
procedure WaitForThread(const BlockIndex: integer);
begin
// while Blocks.Objects[Index] <> nil do
// Sleep(10);
while ThreadManager.ThreadByBlockIndex(BlockIndex) <> nil do
Sleep(10);
end;
function MergeBlocks(const OnProgress: TProgressProc = nil): string;
var
StartBlockCount: integer;
BlockIndex: integer;
BlockMerger: TBlockMergerThread;
FileName1, FileName2, MergedFileName: string;
begin
Result := '';
if Blocks.Count > 0 then
begin
StartBlockCount := Blocks.Count;
while Blocks.Count > 1 do
begin
Blocks.Lock;
try
BlockIndex := Blocks.BlockNumber[0];
FileName1 := Blocks.FileName(BlockIndex);
WaitForThread(BlockIndex);
BlockIndex := Blocks.BlockNumber[1];
FileName2 := Blocks.FileName(BlockIndex);
WaitForThread(BlockIndex);
finally
Blocks.UnLock;
end;
BlockIndex := Blocks.NextBlockNumber;
MergedFileName := Blocks.FileName(BlockIndex);
BlockMerger := TBlockMergerThread.Create(BlockIndex, FileName1, FileName2, MergedFileName);
Blocks.Delete;
Blocks.Delete;
BlockMerger.ResultFileName := Blocks.Add(BlockMerger);
ThreadManager.Run(BlockMerger);
if Assigned(OnProgress) then
OnProgress(0.5 + (1 - Blocks.Count / StartBlockCount) / 2);
end;
Result := Blocks.FileName(Blocks.BlockNumber[0]);
end;
end;
end.