How can I access Mysql under backgroud thread?
How can I access Mysql under backgroud thread?
When I created a TMyConnection in a thread. It doesnt work. For the connection or query must been in a visible form.
Hi
You have to make TMyConnection, TMyQuery and the other object at runtime.
Test exsample:
[/code]
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.
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;
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
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