Einzelnen Beitrag anzeigen

exchange

Registriert seit: 16. Feb 2009
Ort: Erftstadt
20 Beiträge
 
#10

AW: Multithreading (Access Violation)

  Alt 21. Nov 2016, 10:55
Hallo,
vielen Dank für eure Hilfe.
Auf die Memos habe ich auch damals getippt aber die waren es nicht. Habe diese dann drin stehen gelassen. Habe nun eine neue Anwendung gebaut. Erhalte dort aber den gleichen Fehler:

Macht keine Probleme:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
begin
  sleep(1000);
  Thread1.Resume;
  sleep(1000);
  Thread2.Resume;
end;

Macht Probleme:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Thread1.Resume;
  Thread2.Resume;
end;
Darf man einen Thread nicht nacheinander ausführen? Einzelnd funktionieren die Threads auch. Ich habe schon das Forum bei Devart durchsucht, da ich das Problem hier sehe: DBConnection:=TUniConnection.Create(nil);

Kompletter Quellcode:
Delphi-Quellcode:
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.

MFG
  Mit Zitat antworten Zitat