Link to home
Start Free TrialLog in
Avatar of MichaelB082598
MichaelB082598

asked on

Threads And BDE

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?
Avatar of BlackMan
BlackMan

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...
Hey MichaelB, are you still there??
ASKER CERTIFIED SOLUTION
Avatar of BlackMan
BlackMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial