Solved

Threads And BDE

Posted on 1998-09-18
3
410 Views
Last Modified: 2013-11-23
Here is my code:

type
  TDBQueryThread = class(TThread)
  private
    FQuery: TQuery;
    FDataSource: TDataSource;
    FQueryException: Exception;
    procedure HookUpUI;
    procedure QueryError;
  protected
    procedure Execute; override;
  public
    constructor Create(Q: TQuery; D: TDataSource); virtual;
  end;

constructor TDBQueryThread.Create(Q: TQuery; D: TDataSource);
begin
  inherited Create(True);        // create suspended thread
  FQuery := Q;                   // set parameters
  FDataSource := D;
  FreeOnTerminate := True;
  Resume;                        // thread that puppy!
end;

procedure TDBQueryThread.Execute;
begin
  try
    FQuery.Open;                 // open the query
    Synchronize(HookUpUI);       // update UI from main thread
  except
    FQueryException := ExceptObject as Exception;
    Synchronize(QueryError);     // show exception from main thread
  end;
end;

procedure NewQuery(QryNum: integer; Qry: TStrings; const Alias, UserName,
  Password: string);
begin
  { Create a new Query form to show query results }
  with TQueryForm.Create(Application) do
  begin
    { Set a unique session name }
    Session.SessionName := Format('Sess%d', [QryNum]);
    with Database do
    begin
      { set a unique database name }
      DatabaseName := Format('DB%d', [QryNum]);
      { set alias parameter }
      AliasName := Alias;
      { hook database to session }
      SessionName := Session.SessionName;
      { user-defined username and password }
      Params.Values['USER NAME'] := UserName;
      Params.Values['PASSWORD'] := Password;
    end;
    with Query do
    begin
      { hook query to database and session }
      DatabaseName := Database.DatabaseName;
      SessionName := Session.SessionName;
      { set up the query strings }
      SQL.Assign(Qry);
    end;
    { display query strings in SQL Memo }
    memSQL.Lines.Assign(Qry);
    { show query form }
    Show;
    { open query in its own thread }
    TDBQueryThread.Create(Query, DataSource);
  end;
end;

I get a read address violation of: FFFFFFFFFFFFF when it runs the Query.DatabaseName := Database.DatabaseName

Can someone tell me why?
0
Comment
Question by:MichaelB082598
  • 3
3 Comments
 
LVL 7

Expert Comment

by:BlackMan
ID: 1340172
When you are using BDE from a thread, you must create a TSession for each thread and point all the DB stuff in that thread to it. I don't know if it will fix your problem, but it will absolutely fix some others, that you haven't expirenced yet :-)
Don't use a global TSession...
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1340173
Hey MichaelB, are you still there??
0
 
LVL 7

Accepted Solution

by:
BlackMan earned 100 total points
ID: 1340174
When you are using BDE from a thread, you must create a TSession for each thread and point all the DB stuff in that thread to it. I don't know if it will fix your problem, but it will absolutely fix some others, that you haven't expirenced yet :-)
Don't use a global TSession...

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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