unit LoadLookupDataThread;
interface
uses classes;
Type
TOnNewSernr=procedure(sender: TObject; SerNr:
string)
of object;
TOnThreadFinished=procedure(sender: TObject; MaxID: integer)
of object;
TLoadLookupData=class(TThread)
private
FDBHost, FDBUser, FDBPass:
string;
FLastVersionnr: integer;
FOnNewSernr: TOnNewSernr;
FOnTheadFinished: TOnThreadFinished;
procedure DoOnNewSernr(Sernr:
string);
procedure DoOnThreadFinished(MaxID: integer);
public
constructor Create(suspended: boolean; DBHost, DBUser, DBPass:
string;
LastVersionNr: Integer);
protected
procedure Execute;
override;
published
property OnNewSerNr: TOnNewSernr
read FOnNewSernr
write FOnNewSernr;
property OnThreadFinished: TOnThreadFinished
read FOnTheadFinished
write FOnTheadFinished;
end;
implementation
{ TLoadLookupData }
constructor TLoadLookupData.Create(suspended: boolean; DBHost, DBUser,
DBPass:
string; LastVersionNr: Integer);
begin
inherited Create(suspended);
Self.FreeOnTerminate:=True;
Self.NameThreadForDebugging('
LoadLookupDataThread');
FDBHost:=DBHost;
FDBUser:=DBUser;
FDBPass:=DBpass;
FLastVersionnr:=LastVersionNr;
end;
procedure TLoadLookupData.DoOnNewSernr(Sernr:
string);
begin
if Assigned(FOnNewSernr)
then
Synchronize(
procedure
begin
FOnNewSernr(self, Sernr);
end);
end;
procedure TLoadLookupData.DoOnThreadFinished(MaxID: integer);
begin
if Assigned(FOnTheadFinished)
then
Synchronize(
procedure
begin
FOnTheadFinished(self, MaxID);
end);
end;
procedure TLoadLookupData.Execute;
var
FDBConnection: TDBConnection;
FDBQuery: TDBQuery;
MaxID: integer;
begin
MaxID: FLastVersionnr;
FDBConnection:=TDBConnection.Create(
nil);
FDBQuery:=TDBQuery.Create(
nil);
try
FDBConnection.Host:=FDBHost;
FDBConnection.User:=FDBUser;
FDBConnection.Password:=FDBPass;
FDBConnection.connect;
if FDBConnection.connected
then
begin
FDBQuery.Connection:=FDBConnection;
FDBQuery.SQL.Text:='
Select Sernr, Id from Seriennummern where ID>:id';
FDBQuery.Params.parseSQL(FDBQuery.SQL.Text, True);
FDBQuery.Params.paramValue['
id']:=FLastVersionnr;
FDBQuery.Active:=True;
while not FDBQuery.Eof
do
begin
DoOnNewSernr(FDBQuery.Fieldbyname('
Sernr').AsString);
if FDBQuery.Fieldbyname('
id').asInteger>MaxID
then
MaxID:=FDBQuery.Fieldbyname('
id').AsInteger;
FDBQuery.Next;
end;
FDBQuery.Active:=False;
end;
finally
FDBConnection.Free;
FDBQuery.Free;
DoOnThreadFinished(MaxID);
end;
end;
end.