When A DO starts processing data, the application must wait until A D O processing is completed. However, in addition to synchronous execution, A DO also provides an asynchronous execution, allowing the application to continue execution when A D O is processed. When the A D O processing data is completed, A D O will notify the application in an event, and the application can take the corresponding action according to the results executed by A D O. There are many purposes using asynchronous execution, for example, if the application needs to handle a lot of time, then A D O can choose to make A D O on the background to let the application continue to process the graphical user interface or user input data. It is very simple to use asynchronous execution in A D O, and only need to be appropriately set on the E x E C U t e O P T I O N s attribute value of the A D O data set. Now let us explain how to process data in asynchronously with an actual example application. Download 1) Close all items in the D e l P H i integrated development environment. 2) Establish a new application project to connect to the database in the main form of TA D O C O N n e c t i o n object. 3) Put the TA D O D A T A S e t assembly in the main form. The TA D O C O N E C T I O N of the C O N n E C T I O n is set to 2), and the Select * from Adote S T D A T A S is used in its C O m M A N D TE X T property value to obtain all data. Then set its A c t i v e attribute value to TR U E to open the sample data table. 4) Place the TDataSource component, set its DataSet property value to step 3) Added Tadodataset component. 5) Put T D B N A V I g A T O R and T D B G R I D assembly, set their D A T A S O U R C E component to the T D A T A S O U R C e component added in step 4. 6) Put two T B U T T O N components and a T P R O G R e S S B A R component in the main form, and set its related attribute values. At this time, the main form is shown in Figure 3 - 1 2. Figure 3-12 Main Factory 7 of the Example Application is finally placed in the main form. A TA D O C O m M A N D component. Set its c onnection property value to step 2) TA DOC ONNECTION, and use s elec tcount (*) from Adote St D atas in its C OMMAND TE XT property value to from the ADO TE St D ATAS data table Get all the numbers of all data.
8) Write the following program code in the O n ctivate event handler of the main form: Chapter 3 Write an application system using ADO technology (2) 1 1 1 Download Procedure TFORM1.MMACTIVATE (Sender: Tobject); VA RSRECNO : String; begi nprogressbar1.max: = adocommand1.execute.fields.item [0] .value; sRecno: = INTTOSTR (ProgressBar1.max); Self.caption: = 'Commando' SRECNO 'Pen Data'; END; O N a ctivate event handler first executes the SQL command of TA DOC MMAND, and removes the number of all data in the ADO TE St D ATAS table in the temporary R ecordset object, and then specify to TP Rogress B AR The M AX value, finally specifies the c apens attribute value of the form. 9) Double-click the EO A SYNC F etch n on b locking button in the form, and write the following program code in its o n c Lick event handler: Procedure TFORM1.BUTTON2CLICK (Sender: Tobject); Begi Nt R YadodataSet1 .Act: = false; adodataset1.executeOptions: = [eoasyncfetchnonblocking]; Final YLStart: = gettickcount; adodataset1.active: = true; end; end; first close Step 3) TA DOD ATA S ET, Setting its E Xecute O Partions property value to access data in an asynchronous manner. Finally, open the TA D O D A T A SET component of step 3), and acquire data from the A D O TE S T D A T Data. 10) Double-click the EO A SYNC F ETCH button in the form, and write the following program code in its O n c Lick event handler: procedure tform1.button1click (sender: TOBJECT); Begi NT R Yadodataset1.active: = False; adodataset1.executeOptions: = [eoasyncfetch]; Final YLStart: = gettickcount; 1 1 2 Delphi 5.x ADO / MTS / COM advanced programming design Download adodataset1.active: = true; end; end; on top of the program code First turn off step 3) TA DOD ATA S ET, then set its E Xecute O Partions property value to access data using synchronous mode, then open step 3) TA DOD ATA S ET components, from ADO TE ST Data in the D ATAS data table. In an asynchronous mode, A D O notifies the application A D O in an O N f e t C H P R O G R e s S event, and informing the application A d O has been processed by O N f E T C H C O M P L E TEE Event.
Programmers can write program code in these two event handles to handle these two situations. Below is the program code implemented in these two event handles in these two event handles. 11) written at step 3 below) of the TA DOD ata O n F etch P rogress S et event handler program code component: procedure TForm1.ADODataSet1FetchProgress (DataSet: TCustomADODataSet; Progress, MaxProgress: Integer; var EventStatus: TEventStatus) Begi nprogressbar1.position: = progress; end; The above program code is only the display status of the TP Rogress B ASE in the main form when the ADO continues to process data. 12) Write the following program code in the TA DOD ATA S ET component of step 3): Procedure TFORM1.Adodataset1FetCompLETETET1FETCHCOMPLETE (DataSet: tcustomadodataset; constlected); Var Error: TEVENTSTATUS Begi Nlend: = GetTickCount; S How M Essage ('total flower' floattostr ((lend - lstract) / 1000.0) 'second'); END; the above program code is displayed after the ADO processing data The box shows the time spent on the ADO processing data. Now compile and implement this sample application. Figure 3 - 1 3 is a picture of the data table in this example data table in which A D O TE S T D A T D A S is in an asynchronous manner. As can be seen from the figure, when the A D O Acquisition Data Chapter 3 writes an application system using A D O technology (2) 1 3 3 Download the application still continually updates the T P R O G R e s S B A r in the main form. If you are executed in a synchronous manner, T P R O G R E S S B A R cannot be updated so. At this point we can also move the position of the main form, and the application is not possible to continue to work because A D O cannot continue to work due to a large amount of data in access. Figure 3-13 Screen Example Application Executive Executive Mode Figure 3 - 1 4 is a screen displayed when the A D O processing data is completed and the O N f e t c h c O m P L e t-event handler is displayed. From these two images, we can see that when A DO is executed in asynchronously, O N f E T C H P R O G R E S and O N f E T C H C O M P L E TEM can help programmers achieve very useful status information. Figure 3-14 Sample application is performed in asynchronously If you also perform this sample application, you can compare which mode is more efficient when A D O processes data in synchronous execution mode and asynchronous execution mode. Maybe you will be surprised to find that asynchronous execution mode can provide better execution efficiency