The asynchronous execution mode of ADO in Delphi

Source: Internet
Author: User

When ADO begins processing data, the application must wait until ADO has finished processing before it can continue execution. But in addition to synchronous execution, ADO provides a way to execute asynchronously, allowing the application to continue execution as soon as ADO is processed. When ADO processes the data, ADO notifies the application as an event, and the application can then take action based on the results of ADO execution. There are many uses for asynchronous execution, for example, if an application needs to process an action that takes a lot of time, then ADO can choose to have ADO execute asynchronously in the background and let the application continue to process the graphical user interface or user-entered data.
The use of asynchronous execution in ADO is straightforward and requires only the appropriate setting of the ExecuteOptions property value of the ADO DataSet. Now let's take a practical example application to show how ADO can handle data asynchronously.

Download
1) Close all projects in the Delphi integrated development environment.
2) Create a new application project, put the Tadoconnection object in the main form to connect to the
Database.
3) Put the Tadodataset component in the main form. Set its Connection property value to step 2).
Tadoconnection, and uses Select*fromadotestdatas in its CommandText property value
To get all the data. Then set its Active property value to True to open the sample data table.
4) Put the Tdatasource component, set its DataSet property value to step 3) to join the Tadodataset
Component.
5) put the Tdbnavigator and Tdbgrid components, set their datasource components to step 4 plus
into the Tdatasource component.
6) put two TButton components and a Tprogressbar component in the main form, and set it
The value of the associated property. This is shown in main form 3-12.
Figure 3-12 The main form of the sample application
7) Finally put a tadocommand component in the main form. Set its Connection property
Value is tadoconnection of Step 2) and use Select in its CommandText property value
COUNT (*) Fromadotestdatas the number of pens to get all the data from the Adotestdatas data table.
8) Write the following program code in the main form's OnActivate event handler:

The 3rd chapter writes the application system using ADO Technology (II) 111
Download
Proceduretform1.formactivate (Sender:tobject);
Var
srecno:string;
Begin
Progressbar1.max:=adocommand1.execute.fields.item[0]. Value;
Srecno:=inttostr (Progressbar1.max);
self.caption:= ' total ' +srecno+ ' pen data ';
End
The OnActivate event handler first executes the Tadocmmand SQL command, and it callbacks from it
A temporary Recordset object that extracts all the data in the Adotestdatas data table and then specifies
The max value to Tprogressbar, which is then assigned to the Caption property value of the form.
9) Double-click the Eoasyncfetchnonblocking button in the form, and at its onclick event
The following program code is written in the manager program:
Proceduretform1.button2click (Sender:tobject);
Begin
Try
Adodataset1.active:=false;
Adodataset1.executeoptions:=[eoasyncfetchnonblocking];
Finally
Lstart:=gettickcount;
Adodataset1.active:=true;
End
End
In the above program code, first close step 3) Tadodataset, and then set its executeoptions
The property value is used asynchronously to access the data. Finally, open the Tadodataset component of Step 3) from
Data is obtained from the Adotestdatas data table.
10) Double-click the Eoasyncfetch button in the form, and in its OnClick event handler,
Write the following program code:
Proceduretform1.button1click (Sender:tobject);
Begin
Try
Adodataset1.active:=false;
Adodataset1.executeoptions:=[eoasyncfetch];
Finally
Lstart:=gettickcount;
112delphi5.xado/mts/com+ Advanced Programming Chapter
Download
Adodataset1.active:=true;
End
End
In the above program code, first close step 3) Tadodataset, and then set its executeoptions
The Tadodataset component that uses synchronous access to data and then opens step 3)
Data is obtained from the Adotestdatas data table.
In asynchronous mode, ADO notifies the application with the Onfetchprogress event that ADO is still processing
Data, and notifies the application with the Onfetchcomplete event that ADO has processed the data.
Programmers can write program code in these two event handlers to handle both scenarios. Below is the fan
program code that the application implements in both event handlers.

11) Write the following in the Onfetchprogress event handler of the Tadodataset component of Step 3)
The program code:
Proceduretform1.adodataset1fetchprogress (Dataset:tcustomadodataset;
Progress,maxprogress:integer;vareventstatus:teventstatus);
Begin
progressbar1.position:=progress;
End
The above program code just keeps updating the main form when ADO continuously processes the data tprogressbase
The display state.

12) Write the Onfetchcomplete event handler for the Tadodataset component in step 3) as
The following program code:
Proceduretform1.adodataset1fetchcomplete (Dataset:tcustomadodataset;
Consterror:error;vareventstatus:teventstatus);
Begin
Lend:=gettickcount;
ShowMessage (' Total spent ' +floattostr ((lend-lstart)/1000.0) + ' s ');
End
The program code above is that when ADO finishes processing the data, a dialog box appears, displaying the ADO processing
Time spent on data.
Please compile and execute this sample application now. Figure 3-13 is an asynchronous way to access
Adotestdatas the picture of the data in this sample data table. As you can see, when ADO accesses data

The 3rd chapter writes the application system using ADO Technology (II) 113
Download
The Tprogressbar in the main form can still be updated by the application continuously. If you are using synchronous mode
Line, Tprogressbar cannot update the state like this. At this point, we can also move the main form's location, etc., to
See the application does not cause the application to continue to work because ADO accesses large amounts of data.
Figure 3-13 The sample application executes asynchronously in a screen
Figure 3-14 shows when ADO finishes processing the data and triggers the Onfetchcomplete event handler.
Picture. From these two images we can see that when ADO executes asynchronously, the onfetchprogress
and Onfetchcomplete events can help programmers get very useful status information.
Figure 3-14 The sample application executes asynchronously in a screen

If you also execute this sample application, then you can compare when ADO is in synchronous execution mode
and asynchronous execution mode, which mode is more efficient when processing data. You might be surprised to find that async

Asynchronous execution capability

ExecuteOptions
Eoasyncexecute Asynchronous execution
Eoasyncfetch Asynchronous extraction
Eoasyncfetchnonblocking asynchronous extraction without blocking method
Eoexecutenorecords no return record execution

This event is called during Onfetchprogressado execution until the end, which can be used for the progress bar display
This event is called when Onfetchcompleteado execution completes

Reference: http://www.cnblogs.com/key-ok/p/3533426.html

The asynchronous execution mode of ADO in Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.