unit DeleteThread;
interface
uses
Classes, SysUtils,
ActiveX, ADODB;
type
TDeleteThread =
class(TThread)
private
Conn: TADOConnection;
Query: TADOQuery;
protected
procedure Execute;
override;
public
end;
implementation
uses
Main;
{ Wichtig: Methoden und Eigenschaften von Objekten in visuellen Komponenten dürfen
nur in einer Methode namens Synchronize aufgerufen werden, z.B.
Synchronize(UpdateCaption);
und UpdateCaption könnte folgendermaßen aussehen:
procedure TDeleteThread.UpdateCaption;
begin
Form1.Caption := 'Aktualisiert in einem Thread';
end; }
{ TDeleteThread }
procedure TDeleteThread.Execute;
var
Loop: Integer;
begin
CoInitialize(
Nil);
Conn:=TAdoConnection.Create(
Nil);
Conn.ConnectionString:=fMain.ADOConnection1.ConnectionString;
Query:=TAdoQuery.Create(
Nil);
Query.Connection:=Conn;
Conn.ConnectionTimeout := 15;
Query.CommandTimeout := 40;
Loop:=0;
while not Terminated
do
begin
if Loop = (fmain.FDeleteInterval*10)
then begin // Interval-Zeit
if Conn.ConnectionString <> fMain.ADOConnection1.ConnectionString
then
try
Conn.Close;
Conn.ConnectionString:=fMain.ADOConnection1.ConnectionString;
except
end;
if (fmain.FDeleteInterval * 2) > 40
then Query.CommandTimeout := fmain.FDeleteInterval*2;
// doppelte Interval-Zeit in Sekunden
if fMain.FmaxEntries = true
then
begin
if (fMain.FautoDelete = true)
then
begin
fmain.Tracer.Logdebug('
DeleteThread, maxEntries: '+IntToStr(fmain.FDBmaxEntries)+'
, autodelete: '+IntToStr(fmain.FDBAutoDelete));
Query.SQL.Text := '
If (Select Count(*) from GsfOPCLog) > ' + IntToStr(fMain.FDBmaxEntries + fMain.FDBAutoDelete) + '
begin'#13#10 +
'
Delete top(' + IntToStr(fMain.FDBAutoDelete) + '
) from GsfOPCLog'#13#10 +
'
where ID not in (Select top ' + IntToStr(fMain.FDBmaxEntries) + '
ID from GsfOpcLog order by Timestamp desc)'#13#10 +
'
end';
try
Query.ExecSQL;
fmain.Tracer.LogDebug('
Query.SQL.Text: '+
query.SQL.Text);
except
on E:
Exception do fmain.AddToLog('
DeleteThread','
Error in DeleteThread.Execute: '+E.
Message,1,7);
end;
end;
end;
Loop:=0;
end;
Sleep(100);
Inc(Loop);
end;
Query.Free;
Conn.Free;
CoUninitialize;
end;
end.