unit Unit8;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
ActiveX, Data.Win.ADODB;
type
TWorkerThread = class(TThread)
private
txt : String;
ADOConnection : TADOConnection;
procedure Sync;
public
constructor create;
destructor destroy; override;
procedure execute; override;
end;
//--------------------------------------------
TForm8 = class(TForm)
btnStart : TButton;
mmo : TMemo;
procedure btnStartClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
fStarted : Boolean;
fThread : TWorkerThread;
public
{ Public-Deklarationen }
end;
//--------------------------------------------
var
Form8: TForm8;
implementation
{$R *.dfm}
procedure TForm8.FormShow(Sender: TObject);
begin
fStarted := false;
mmo.Clear;
ReportMemoryLeaksOnShutdown := true;
end;
procedure TForm8.btnStartClick(Sender: TObject);
begin
if fStarted then
begin
fThread.Terminate;
fThread.WaitFor;
FreeAndNil(fThread);
fStarted := false;
btnStart.Caption := 'Start';
end
else
begin
fThread := TWorkerThread.Create();
fThread.start;
fStarted := true;
btnStart.Caption := 'Stop';
end;
end;
// TWorkerThread -------------------------
constructor TWorkerThread.create;
begin
inherited create(true);
FreeOnTerminate := false;
txt := 'created';
Synchronize(Sync);
end;
destructor TWorkerThread.destroy;
begin
txt := 'destroyed';
Synchronize(Sync);
inherited;
end;
procedure TWorkerThread.execute;
begin
inherited;
try
CoInitialize(nil);
try
ADOConnection := TADOConnection.Create(nil);
ADOConnection.ConnectionString := 'Provider=MSDASQL.1;Password=PASSWORD;Persist Security Info=True;User ID=ADMIN;Data Source=
DB';
ADOConnection.Open;
finally
sleep(1000);
ADOConnection.Close;
FreeAndNil(ADOConnection);
CoUninitialize;
end;
except
on e:
Exception do
begin
txt := e.Message;
Synchronize(Sync);
end;
end;
end;
procedure TWorkerThread.Sync;
begin
Form8.mmo.Lines.Add(DateTimeToStr(now) + ' | ' + txt)
end;
end.