DELPHI XE3 ADO Query makes my application frozen while it waits for data

Is there any solution not to disable my user interface while it is waiting for data from the database?

Example:

We have adoquery and we do

adoquery.active: = false; adoquery.active: = true;

When adoquery tries to get data from db, everything in the user interface is frozen, and if the user clicks, the whole program becomes Unresponsive!

Is there any cure for this problem?

+1
delphi ado
source share
1 answer

You can use [eoAsyncExecute,eoAsyncFetch] in ExecuteOptions , this will require that an explicit TAdoConnection be used in the dataset.
To avoid unexpected behavior, you will need to use DisableControls before opening the dataset and EnableControls after the FetchComplete .
A direct call to EnableControls inside the FetchComplete may result in Exceptions, so using Postmessage with a user-defined message will be necessary.

  Const // define a message for handling completed AsyncFetch after leaving FetchComplete WM_MYConnect=WM_User + 77; type TMyForm = class(TForm) MyDataSet: TADODataSet; MyDatasource: TDataSource; DBGrid1: TDBGrid; Button1: TButton; ADOConnection1: TADOConnection; procedure Button1Click(Sender: TObject); procedure MyDataSetFetchComplete(DataSet: TCustomADODataSet; const Error: Error; var EventStatus: TEventStatus); private { Private-Deklarationen } Procedure ConnectDatasource(var MSG:TMessage); message WM_MYConnect; public { Public-Deklarationen } end; var MyForm: TMyForm; implementation {$R *.dfm} procedure TMyForm.Button1Click(Sender: TObject); begin MyDataset.Close; // example blocking command for SQL-Server 10 seconds MyDataset.CommandText := 'WAITFOR DELAY ''00:00:10'' Select* from aa'; Mydataset.DisableControls; Mydataset.ExecuteOptions := [eoAsyncExecute,eoAsyncFetch]; MyDataset.Open; end; procedure TMyForm.ConnectDatasource(var MSG:TMessage); begin TAdodataset(MSG.WParam).EnableControls; // MyDataSource.DataSet := MyDataset; end; procedure TMyForm.MyDataSetFetchComplete(DataSet: TCustomADODataSet; const Error: Error; var EventStatus: TEventStatus); begin // if we want to add a datasource we will have to ensure that it will happen after leaving FetchComplete // so we call our procedure ConnectDatasource via PostMessage PostMessage(Handle,WM_MYConnect,wParam(DataSet),0); end; end. 
+5
source share

All Articles