This is the followup of my previous post . I now have a few ADOQueries in the Procedure and one ADOConnection . All of these are Dynamically Created and Freed .
QUESTION : I was hoping that this entire Compare Procedure would be a lot faster if I use 4 Threads , but instead I have the same speed as without Threads, only it is a lot more complicated now . Or maybe I am doing something completely wrong here ?
The Code is here , sorry I know it is a bit to much , but without the code my Question makes no sense and the answer would only be guessing . MSSQL is not wrapped in a Try Finally I know , I just wanted first to check if there is any speed gain here .
Entire Procedure pasted as asked . I also altered it I do the CREATE of Objects outside the Loop and the Free After the Loop .
procedure TMain.SYNC(AProgressBar: TProgressBar; AData : array of RemoteDATA);
var i : integer;
isFound : boolean;
LStatus, LSU_Stueck, LHistLines , LPosLines, LSTLLines, LTXTLines, LKTXTLines : integer;
ABQuery , HistQuery, PosQuery, STLQuery, TXTQuery, KTXTQuery : TADOQuery;
MSSQL : TADOConnection;
begin
MSSQL := TADOConnection.Create(nil);
MSSQL.ConnectionString:='FILE NAME='+ExtractFilePath(Application.ExeName)+'\xlr_main.udl';
MSSQL.Provider:='SQLOLEDB.1';
MSSQL.KeepConnection:=true;
MSSQL.LoginPrompt:=false;
ABQuery := TADOQuery.Create(nil);
ABQuery.Connection:=MSSQL;
HistQuery := TADOQuery.Create(nil);
HistQuery.Connection:=MSSQL;
PosQuery := TADOQuery.Create(nil);
PosQuery.Connection:=MSSQL;
STLQuery := TADOQuery.Create(nil);
STLQuery.Connection:=MSSQL;
TXTQuery := TADOQuery.Create(nil);
TXTQuery.Connection:=MSSQL;
KTXTQuery := TADOQuery.Create(nil);
KTXTQuery.Connection:=MSSQL;
for i := Low(AData) to High(AData) do
begin
isFound:=false;
LStatus:=0;
LSU_Stueck:=0;
LHistLines:=0;
LPosLines:=0;
LSTLLines:=0;
LTXTLines:=0;
LTXTLines:=0;
ABQuery.SQL.Clear;
ABQuery.SQL.Add('select AB,STATUS,SU_STUECK,DB_YEAR from BW_AUFTR_KOPF where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
ABQuery.Open;
if ABQuery.RecordCount <> 0 then
begin
isFound:=true;
LStatus:=ABQuery.FieldByName('Status').AsInteger;
LSU_Stueck:=ABQuery.FieldByName('SU_STUECK').AsInteger;
end;
HistQuery.SQL.Clear;
HistQuery.SQL.Add('select COUNT(Datum) as HistLines from BW_AUFTR_HIST where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
HistQuery.Open;
LHistLines:=HistQuery.FieldByName('HistLines').AsInteger;
PosQuery.SQL.Clear;
PosQuery.SQL.Add('select COUNT(POS_NR) as PosLines from BW_AUFTR_POS where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
PosQuery.Open;
LPosLines:=PosQuery.FieldByName('PosLines').AsInteger;
STLQuery.SQL.Clear;
STLQuery.SQL.Add('select COUNT(POS_NR) as STLLines from BW_AUFTR_STL where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
STLQuery.Open;
LSTLLines:=STLQuery.FieldByName('STLLines').AsInteger;
TXTQuery.SQL.Clear;
TXTQuery.SQL.Add('select COUNT(POS_NR) as TXTLines from BW_AUFTR_TXT where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
TXTQuery.Open;
LTXTLines:=TXTQuery.FieldByName('TXTLines').AsInteger;
KTXTQuery.SQL.Clear;
KTXTQuery.SQL.Add('select COUNT(LFD_NR) as KTXTLines from BW_AUFTR_KTXT where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
KTXTQuery.Open;
LKTXTLines:=KTXTQuery.FieldByName('KTXTLines').AsInteger;
if isFound = true then
begin
if (AData[i].STATUS <> LStatus) or (AData[i].SU_STUECK <> LSU_Stueck)
or (AData[i].HISTLINES <> LHistLines) or (AData[i].POSLINES <> LPosLines) or (AData[i].STLLINES <> LSTLLines)
or (AData[i].TXTLINES <> LTxtLines) or (AData[i].KTXTLINES <> LKTXTLines) then
if (AData[i].STATUS < 100) and (AData[i].STATUS <> 98) then
begin
setlength(CHANGED_ARRAY,length(CHANGED_ARRAY)+1);
CHANGED_ARRAY[High(CHANGED_ARRAY)].AB:=AData[i].AB;
CHANGED_ARRAY[High(CHANGED_ARRAY)].DB_YEAR:=AData[i].DB_YEAR;
end;
if (AData[i].STATUS = 98) or (AData[i].STATUS = 117) or (AData[i].STATUS = 900)
or (AData[i].STATUS = 999) then
begin
setlength(DELETE_ARRAY,length(DELETE_ARRAY)+1);
DELETE_ARRAY[High(DELETE_ARRAY)].AB:=AData[i].AB;
DELETE_ARRAY[High(DELETE_ARRAY)].DB_YEAR:=AData[i].DB_YEAR;
end;
end
else
begin
if (AData[i].STATUS <> 98) and (AData[i].STATUS <> 117) and (AData[i].STATUS <> 900)
and (AData[i].STATUS <> 999) and (AData[i].STATUS <> 120) then
begin
setlength(NEW_ARRAY,length(NEW_ARRAY)+1);
NEW_ARRAY[High(NEW_ARRAY)].AB:=AData[i].AB;
NEW_ARRAY[High(NEW_ARRAY)].DB_YEAR:=AData[i].DB_YEAR;
end;
end;
TThread.Queue(TThread.CurrentThread,
procedure
begin
AProgressBar.Position:=i;
end);
end;
ABQuery.Free;
HistQuery.Free;
PosQuery.Free;
STLQuery.Free;
TXTQuery.Free;
KTXTQuery.Free;
MSSQL.Free;
end;
And I start the Threads here , then I wait for it to complete , but I also must show the progress .
if length(RAB_ARRAY) > 10 then
begin
Edit1.Text:='Items '+inttostr(length(RAB_ARRAY))+' 1/4 '+inttostr(length(RAB_ARRAY) div 4)+' LeftOver '+inttostr(length(RAB_ARRAY) mod 4);
Part:=length(RAB_ARRAY) div 4;
LeftOver:=length(RAB_ARRAY) mod 4;
setlength(Tasks,4);
Tasks[0] := TTask.Create(
procedure
begin
SYNC(progressThread1,copy(RAB_ARRAY,0,Part))
end);
Tasks[1] := TTask.Create(
procedure
begin
SYNC(progressThread2,copy(RAB_ARRAY,Part-1,Part))
end);
Tasks[2] := TTask.Create(
procedure
begin
SYNC(progressThread3,copy(RAB_ARRAY,(2*Part)-2,Part))
end);
Tasks[3] := TTask.Create(
procedure
begin
SYNC(progressThread4,copy(RAB_ARRAY,(3*Part)-3,Part+LeftOver))
end);
progressThread1.Max:=Part;
progressThread2.Max:=Part;
progressThread3.Max:=Part;
progressThread4.Max:=Part+LeftOver;
Tasks[0].Start;
Tasks[1].Start;
Tasks[2].Start;
Tasks[3].Start;
while true do
begin
if (Tasks[0].Status = TTaskStatus.Completed) and (Tasks[1].Status = TTaskStatus.Completed)
and (Tasks[2].Status = TTaskStatus.Completed) and (Tasks[3].Status = TTaskStatus.Completed) then
break;
Application.ProcessMessages;
end;