home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue25 / speed / UnitThreads.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-05-18  |  3.3 KB  |  111 lines

  1. unit UnitThreads;
  2.  
  3. interface
  4.    uses SysUtils, Forms, Classes, DB, DBTables, DBGrids;
  5.  
  6. type
  7.    TThreadedQuery = class(TThread)
  8.      private
  9.         FID:                  string;
  10.         FGrid:                TDBGrid;
  11.         SessionThread:        TSession;
  12.         DatabaseThread:       TDatabase;
  13.         QueryThread:          TQuery;
  14.         DataSourceThread:     TDataSource;
  15.         FExecuteException:        Exception;
  16.         procedure LinkDataSourceToTDBGrid;
  17.         procedure ShowExecuteException;
  18.      protected
  19.         procedure Execute; override;
  20.      public
  21.         constructor Create (const ID, Alias, UserName, Password: string;
  22.                            QueryStatement: TStrings; Grid: TDBGrid);
  23.                    virtual;
  24.         destructor Destroy; override;
  25.    end;
  26.  
  27. implementation
  28.  
  29. uses UnitStartTwoThreads;
  30.  
  31. constructor TThreadedQuery.Create
  32. (const ID, Alias, UserName, Password: string; QueryStatement: TStrings; Grid: TDBGrid);
  33. begin
  34.    // Create Thread in a Suspended State
  35.    inherited Create(True);
  36.    // The rest of the Create processing is still using
  37.    // the primary thread so VCL's can be accessed.
  38.    FID:=       ID;
  39.    FGrid:=      Grid;
  40.    // Create object instances
  41.    SessionThread:=   TSession.Create(nil);
  42.    DatabaseThread:=  TDatabase.Create(nil);
  43.    QueryThread:=     TQuery.Create(nil);
  44.    DataSourceThread:= TDataSource.Create(nil);
  45.    // Give the new Session object a unique name
  46.    SessionThread.SessionName:= 'Session'+ID;
  47.    with DatabaseThread do begin
  48.       // Give the new Database object a unique name
  49.       DatabaseName:=   'DatabaseName'+ID;
  50.       // Link the Session object to the Database object
  51.       SessionName:=    SessionThread.SessionName;
  52.       AliasName:=      Alias;
  53.       Params.Values['USER NAME']:=  UserName;
  54.       Params.Values['PASSWORD'] :=  Password;
  55.       LoginPrompt := False;
  56.    end;
  57.    with QueryThread do begin
  58.       // Link the Database object to the Query object
  59.       DatabaseName:= DatabaseThread.DatabaseName;
  60.       // Line the Session object to the Query object
  61.       SessionName:= SessionThread.SessionName;
  62.       // Assign the actual SQL Query to the Query object
  63.       SQL.Assign(QueryStatement);
  64.    end;
  65.    // Don't allow the thread to terminate it self when complete.
  66.    FreeOnTerminate:= False;
  67.    //  Resume execution of Thread
  68.    Resume;
  69. end;
  70.  
  71. destructor TThreadedQuery.Destroy;
  72. begin
  73.    QueryThread.Close;
  74.    DataSourceThread.Free;
  75.    QueryThread.Free;
  76.    DatabaseThread.Free;
  77.    SessionThread.Free;
  78.    inherited Destroy;
  79. end;
  80.  
  81. procedure TThreadedQuery.Execute;
  82. begin
  83.    try
  84.       // Execute the query
  85.       QueryThread.Open;
  86.       // Link the Query Object to the DataSource Object
  87.       DataSourceThread.DataSet:= QueryThread;
  88.       DataSourceThread.Enabled:= True;
  89.       Synchronize (LinkDataSourceToTDBGrid);
  90.    except
  91.       // Save the current exception object
  92.       FExecuteException:= ExceptObject as Exception;
  93.       Synchronize (ShowExecuteException);
  94.    end;
  95. end;
  96.  
  97. procedure TThreadedQuery.LinkDataSourceToTDBGrid;
  98. begin
  99.      // Link the DataSource Object to the Forms DBGrid
  100.      FGrid.DataSource:= DataSourceThread;
  101. end;
  102.  
  103. procedure TThreadedQuery.ShowExecuteException;
  104. begin
  105.      // Show error message
  106.      Application.ShowException(FExecuteException);
  107. end;
  108.  
  109.  
  110. end.
  111.