问题描述
有什么解决方案可以避免在等待数据从数据库中冻结时冻结我的UI吗?
Is there any solution so as not to get my UI frozen while it waits for data to come from the Database ??
示例:
我们有一个adoquery,我们这样做
We have an adoquery and we do
adoquery.active:= false;
adoquery.active:= true;
adoquery.active:=false; adoquery.active:=true;
当adoquery尝试从db获取数据时,UI中的所有内容都会冻结,如果用户单击,则整个程序变成
不响应!
When the adoquery tries to get data from the db everything in the UI is frozen and if the user clicks then the whole program becomesNot Responding!!
这个问题有任何解决方法吗?
Is there any cure to this problem ?
推荐答案
您可以在 ExecuteOptions
中使用 [eoAsyncExecute,eoAsyncFetch]
,这需要数据集正在使用显式TAdoConnection。
为避免意外行为,您必须在打开数据集之前使用 DisableControls
和 EnableControls
在 FetchComplete
之后。
<$内的直接调用 EnableControls
c $ c> FetchComplete 可能会导致异常,因此必须将Postmessage与用户定义的消息一起使用。
You might use [eoAsyncExecute,eoAsyncFetch]
in the ExecuteOptions
, this will require the Dataset is using an explicit TAdoConnection.
To avoid unexpected behavior you will have to use DisableControls
before opening the Dataset, and EnableControls
after FetchComplete
.
A direct called EnableControls
within FetchComplete
may cause Exceptions, so using Postmessage with an 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.
这篇关于DELPHI XE3 ADO查询使我的应用程序在等待数据时冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!