jamesr123456
asked on
IBX component in thread
Hi,
I'm new to using threads and am looking for an example of how to do the following:
I want a blank form with a button. When I press the button I want to start a thread that creates a tibdatabase component, opens the connection, and then shows a message saying successful.
I'm new to using threads and am looking for an example of how to do the following:
I want a blank form with a button. When I press the button I want to start a thread that creates a tibdatabase component, opens the connection, and then shows a message saying successful.
So just go to File -> New -> Other -> Thread Object and put the application code into the Execute procedure, being certain that you use Synchronize to make ANY references to GUI components.
Oh, and yes, within your button's OnClick event, you could do the creation (dynamically).
ASKER
I've tried doing that, but it just does nothing. I get no error messages either.
Would you mind posting your code?
ASKER
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DB, IBDatabase;
type TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; { of class declaration }
type
mythread = class(TThread)
private
{ Private declarations }
procedure opendatabase;
protected
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender : TObject);
begin
mythread.Create(false);
end; { of procedure }
procedure mythread.Execute;
begin
{ Place thread code here }
synchronize(opendatabase);
end;
procedure mythread.opendatabase;
var db:tibdatabase;
begin
db:=tibdatabase.Create(db) ;
db.DatabaseName:='z:\track .gdb';
db.Params.add('user_name=s ysdba');
db.params.add('password=ma sterkey');
db.Open;
if db.connected then form1.label1.caption:='con nected';
end;
end.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DB, IBDatabase;
type TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; { of class declaration }
type
mythread = class(TThread)
private
{ Private declarations }
procedure opendatabase;
protected
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender
begin
mythread.Create(false);
end; { of procedure }
procedure mythread.Execute;
begin
{ Place thread code here }
synchronize(opendatabase);
end;
procedure mythread.opendatabase;
var db:tibdatabase;
begin
db:=tibdatabase.Create(db)
db.DatabaseName:='z:\track
db.Params.add('user_name=s
db.params.add('password=ma
db.Open;
if db.connected then form1.label1.caption:='con
end;
end.
The problem is in the ButtonClick.
procedure TForm1.Button1Click(Sender : TObject);
var
this MyThread : mythread;
begin
thismythread := mythread.Create(false);
end; { of procedure }
procedure TForm1.Button1Click(Sender
var
this MyThread : mythread;
begin
thismythread := mythread.Create(false);
end; { of procedure }
ASKER
still dosnt work. the label changes to 'starting' and stays like that
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DB, IBDatabase;
type TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; { of class declaration }
type
mythread = class(TThread)
private
{ Private declarations }
procedure opendatabase;
protected
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender : TObject);
var f:mythread;
begin
f:=mythread.Create(false);
end; { of procedure }
procedure mythread.Execute;
begin
{ Place thread code here }
synchronize(opendatabase);
end;
procedure mythread.opendatabase;
var db:tibdatabase;
begin
form1.label1.caption:='sta rting';
db:=tibdatabase.Create(db) ;
db.DatabaseName:='z:\track \protrack. gdb';
db.Params.add('user_name=s ysdba');
db.params.add('password=ma sterkey');
form1.label1.caption:='abo ut to open';
db.Open;
if db.connected then form1.label1.caption:='con nected' else form1.Label1.caption:='dis connected' ;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DB, IBDatabase;
type TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; { of class declaration }
type
mythread = class(TThread)
private
{ Private declarations }
procedure opendatabase;
protected
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender
var f:mythread;
begin
f:=mythread.Create(false);
end; { of procedure }
procedure mythread.Execute;
begin
{ Place thread code here }
synchronize(opendatabase);
end;
procedure mythread.opendatabase;
var db:tibdatabase;
begin
form1.label1.caption:='sta
db:=tibdatabase.Create(db)
db.DatabaseName:='z:\track
db.Params.add('user_name=s
db.params.add('password=ma
form1.label1.caption:='abo
db.Open;
if db.connected then form1.label1.caption:='con
end;
end.
Does the same code work successfully in a non-thread procedure?
ASKER
yes. works fine if its not in a thread
Hmm. This will take some experimenting. But not today I'm afraid.
ASKER
Okay not to worry, i've sorted it out.
I've upgraded to the latest IBX, and also found an example of how to use IBconnectionbroker in a thread.
I've upgraded to the latest IBX, and also found an example of how to use IBconnectionbroker in a thread.
As the only responder, I have no objection, so don't bother waiting on my behalf.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My pleasure. But I would appreciate some attention being given to https://www.experts-exchange.com/questions/20912205/Handled-Abandoned-question.html