Solved

IBX component in thread

Posted on 2004-04-30
15
181 Views
Last Modified: 2010-04-05
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.


0
Comment
Question by:jamesr123456
  • 8
  • 5
15 Comments
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
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.
0
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
Oh, and yes, within your button's OnClick event, you could do the creation (dynamically).
0
 

Author Comment

by:jamesr123456
Comment Utility
I've tried doing that, but it just does nothing. I get no error messages either.
0
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
Would you mind posting your code?
0
 

Author Comment

by:jamesr123456
Comment Utility
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=sysdba');
db.params.add('password=masterkey');
db.Open;
if db.connected then form1.label1.caption:='connected';
end;

end.

0
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
The problem is in the ButtonClick.

procedure TForm1.Button1Click(Sender: TObject);
var
  this MyThread : mythread;
begin
  thismythread := mythread.Create(false);
end; { of procedure }
0
 

Author Comment

by:jamesr123456
Comment Utility
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:='starting';
db:=tibdatabase.Create(db);
db.DatabaseName:='z:\track\protrack.gdb';
db.Params.add('user_name=sysdba');
db.params.add('password=masterkey');
form1.label1.caption:='about to open';
db.Open;
if db.connected then form1.label1.caption:='connected' else form1.Label1.caption:='disconnected';

end;

end.

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 7

Expert Comment

by:sftweng
Comment Utility
Does the same code work successfully in a non-thread procedure?
0
 

Author Comment

by:jamesr123456
Comment Utility
yes. works fine if its not in a thread
0
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
Hmm. This will take some experimenting. But not today I'm afraid.
0
 

Author Comment

by:jamesr123456
Comment Utility
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.
0
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
As the only responder, I have no objection, so don't bother waiting on my behalf.
0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
PAQ-ing the question and refunding 50  points

Thanks sftweng !

modulo

Community Support Moderator
Experts Exchange
0
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
My pleasure. But I would appreciate some attention being given to http://www.experts-exchange.com/Community_Support/Q_20912205.html
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now