How can I access Mysql under backgroud thread?

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
wickw

How can I access Mysql under backgroud thread?

Post by wickw » Sat 01 Jan 2005 11:07

When I created a TMyConnection in a thread. It doesnt work. For the connection or query must been in a visible form.

TheLion
Posts: 39
Joined: Thu 25 Nov 2004 11:28
Location: Copenhagen/Denmark

Post by TheLion » Sun 02 Jan 2005 16:18

Hi :-)

You have to make TMyConnection, TMyQuery and the other object at runtime.

Test exsample:

Code: Select all

unit UnitTest;

interface

uses
  SySUtils, Classes, Db, MemDS, DBAccess, MyAccess;

type
  Test = class(TThread)
  private
    fMyConnection: TMyConnection;
    fMyQuery: TMyQuery;
    fOwner: TComponent;
  protected
    procedure Execute; override;
  public
    constructor Create;
    destructor  Destroy; override;
  end;

implementation

{ Important: Methods and properties of objects in VCL can only be used in a
  method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure Test.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ Test }

constructor Test.Create;
begin
  fMyConnection                   := TMyConnection.Create(fOwner);
  fMyQuery                        := TMyQuery.Create(fOwner);
  fMyQuery.Connection             := fMyConnection;
  fMyConnection.ConnectionTimeout := 15;
  fMyConnection.Database          := 'Database';
  fMyConnection.Password          := 'Password';
  fMyConnection.Port              := 3306;
  fMyConnection.Server            := 'Server';
  fMyConnection.Username          := 'Username';
end;

destructor Test.Destroy;
begin
  FreeAndNil(fMyConnection);
  FreeAndNil(fMyQuery);
end;

procedure Test.Execute;
begin
  { Place thread code here }
end;

end.
[/code]

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Wed 05 Jan 2005 09:57

At the example that you specified there are some errors. It must be

Code: Select all

constructor Test.Create;
begin
  inherited Create(True);
  fMyConnection                   := TMyConnection.Create(fOwner);
  fMyQuery                        := TMyQuery.Create(fOwner);
  fMyQuery.Connection             := fMyConnection;
  fMyConnection.ConnectionTimeout := 15;
  fMyConnection.Database          := 'Database';
  fMyConnection.Password          := 'Password';
  fMyConnection.Port              := 3306;
  fMyConnection.Server            := 'Server';
  fMyConnection.Username          := 'Username';
  Resume;
end;

destructor Test.Destroy;
begin
  FreeAndNil(fMyConnection);
  FreeAndNil(fMyQuery);
  inherited;
end;

TheLion
Posts: 39
Joined: Thu 25 Nov 2004 11:28
Location: Copenhagen/Denmark

Post by TheLion » Wed 05 Jan 2005 12:10

HI :-)

Yes I found that out last night. The "inherited" statement in Test.Destroy I just forgot. Sorry. But the "Inherited Create(True)" and "Resume" I just found that out myself, last night. If I don't do that, the Test.Execute statement get executed before the Test.Create part is done. Wierd but true.
Sorry for being a little fast on the keyboard :-)

Post Reply