home *** CD-ROM | disk | FTP | other *** search
- unit UnitThreads;
-
- interface
- uses SysUtils, Forms, Classes, DB, DBTables, DBGrids;
-
- type
- TThreadedQuery = class(TThread)
- private
- FID: string;
- FGrid: TDBGrid;
- SessionThread: TSession;
- DatabaseThread: TDatabase;
- QueryThread: TQuery;
- DataSourceThread: TDataSource;
- FExecuteException: Exception;
- procedure LinkDataSourceToTDBGrid;
- procedure ShowExecuteException;
- protected
- procedure Execute; override;
- public
- constructor Create (const ID, Alias, UserName, Password: string;
- QueryStatement: TStrings; Grid: TDBGrid);
- virtual;
- destructor Destroy; override;
- end;
-
- implementation
-
- uses UnitStartTwoThreads;
-
- constructor TThreadedQuery.Create
- (const ID, Alias, UserName, Password: string; QueryStatement: TStrings; Grid: TDBGrid);
- begin
- // Create Thread in a Suspended State
- inherited Create(True);
- // The rest of the Create processing is still using
- // the primary thread so VCL's can be accessed.
- FID:= ID;
- FGrid:= Grid;
- // Create object instances
- SessionThread:= TSession.Create(nil);
- DatabaseThread:= TDatabase.Create(nil);
- QueryThread:= TQuery.Create(nil);
- DataSourceThread:= TDataSource.Create(nil);
- // Give the new Session object a unique name
- SessionThread.SessionName:= 'Session'+ID;
- with DatabaseThread do begin
- // Give the new Database object a unique name
- DatabaseName:= 'DatabaseName'+ID;
- // Link the Session object to the Database object
- SessionName:= SessionThread.SessionName;
- AliasName:= Alias;
- Params.Values['USER NAME']:= UserName;
- Params.Values['PASSWORD'] := Password;
- LoginPrompt := False;
- end;
- with QueryThread do begin
- // Link the Database object to the Query object
- DatabaseName:= DatabaseThread.DatabaseName;
- // Line the Session object to the Query object
- SessionName:= SessionThread.SessionName;
- // Assign the actual SQL Query to the Query object
- SQL.Assign(QueryStatement);
- end;
- // Don't allow the thread to terminate it self when complete.
- FreeOnTerminate:= False;
- // Resume execution of Thread
- Resume;
- end;
-
- destructor TThreadedQuery.Destroy;
- begin
- QueryThread.Close;
- DataSourceThread.Free;
- QueryThread.Free;
- DatabaseThread.Free;
- SessionThread.Free;
- inherited Destroy;
- end;
-
- procedure TThreadedQuery.Execute;
- begin
- try
- // Execute the query
- QueryThread.Open;
- // Link the Query Object to the DataSource Object
- DataSourceThread.DataSet:= QueryThread;
- DataSourceThread.Enabled:= True;
- Synchronize (LinkDataSourceToTDBGrid);
- except
- // Save the current exception object
- FExecuteException:= ExceptObject as Exception;
- Synchronize (ShowExecuteException);
- end;
- end;
-
- procedure TThreadedQuery.LinkDataSourceToTDBGrid;
- begin
- // Link the DataSource Object to the Forms DBGrid
- FGrid.DataSource:= DataSourceThread;
- end;
-
- procedure TThreadedQuery.ShowExecuteException;
- begin
- // Show error message
- Application.ShowException(FExecuteException);
- end;
-
-
- end.
-