unit main;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls, Uni, MySQLUniProvider;
type
TForm1 =
class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
TThread1 =
class(TThread)
protected
procedure Execute;
override;
end;
TThread2 =
class(TThread)
protected
procedure Execute;
override;
end;
var
Form1: TForm1;
MySQL: TMySQLUniProvider;
Thread1: TThread1;
Thread2: TThread2;
implementation
{$R *.dfm}
procedure TThread1.Execute;
var DBConnection: TUniConnection; DBQuery: TUniQuery;
begin
while not Terminated
do
begin
DBConnection:=TUniConnection.Create(
nil);
DBConnection.Server:='
172.16.6.50';
DBConnection.Port:=3306;
DBConnection.Username:='
root_copy';
DBConnection.Password:='
password';
DBConnection.ProviderName:='
MySQL';
DBConnection.Database:='
testdb';
DBConnection.Connect;
if DBConnection.Connected
then
begin
DBQuery:=TUniQuery.Create(
nil);
DBQuery.Connection:=DBConnection;
DBQuery.SQL.Text:='
INSERT into test SET datum=NOW(), thread=1;';
DBQuery.Execute;
DBQuery.Free;
DBConnection.Close;
end;
DBConnection.Free;
sleep(2000);
end;
end;
procedure TThread2.Execute;
var DBConnection: TUniConnection; DBQuery: TUniQuery;
begin
while not Terminated
do
begin
DBConnection:=TUniConnection.Create(
nil);
DBConnection.Server:='
172.16.6.50';
DBConnection.Port:=3306;
DBConnection.Username:='
root_copy';
DBConnection.Password:='
password';
DBConnection.Database:='
testdb';
DBConnection.ProviderName:='
MySQL';
DBConnection.Connect;
if DBConnection.Connected
then
begin
DBQuery:=TUniQuery.Create(
nil);
DBQuery.Connection:=DBConnection;
DBQuery.SQL.Text:='
INSERT into test SET datum=NOW(), thread=2;';
DBQuery.Execute;
DBQuery.Free;
DBConnection.Close;
end;
DBConnection.Free;
sleep(2000);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sleep(1000);
Thread1.Resume;
sleep(1000);
Thread2.Resume;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
MySQL.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MySQL:=TMySQLUniProvider.Create(
nil);
Thread1 := TThread1.Create(True);
Thread1.FreeOnTerminate := True;
Thread2 := TThread2.Create(True);
Thread2.FreeOnTerminate := True;
end;
end.