How can I access Mysql under backgroud thread?

How can I access Mysql under backgroud thread?

Postby 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.
wickw
 

Postby 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]
TheLion
 
Posts: 39
Joined: Thu 25 Nov 2004 11:28
Location: Copenhagen/Denmark

Postby 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;
Ikar
 
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Postby 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 :-)
TheLion
 
Posts: 39
Joined: Thu 25 Nov 2004 11:28
Location: Copenhagen/Denmark


Return to MySQL Data Access Components