Asynchronous processing in ADO. Net-poll

Source: Internet
Author: User

Ado. Net has three asynchronous Processing Methods: poll, wait, and function callback.

 

Poll method: This method starts the asynchronous process, and then selects the iasyncresult object to see when the process is completed.

Wait: This method associates the wait handle with an asynchronous process. Wait until all or part of the processes are completed so that they can be processed accordingly.

 

 

Function callback method: When the asynchronous call ends, the callback function is triggered and the result set is displayed on the screen.

 

I have never used asynchronous processing. Some people on the Internet say that it can be used when a large amount of data is used or when multiple processes are executed together. This requires further research.

 

Poll usage

 

UseCode.

Put a gridview on the page to fetch data.

 

< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
< ASP: gridview ID = "Gvdata" Runat = "Server" >
</ ASP: gridview >
</ Div >
</ Form >
</ Body >

Use poll to retrieve data.

Using System. Data;
Using System. Data. sqlclient;
Using System. configuration;

Namespace Adoasyncdb
{
///   <Summary>
/// Poll Method
///   </Summary>
Public   Partial   Class Pollmethod: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Usepollmethod ();
}

Private VoidUsepollmethod ()
{
Sqlconnection connection;
Sqlcommand= NewSqlcommand ();
Sqldatareader datareader;
Iasyncresult asyncresult;

Response. Write (" ProgramStarted ...... <Br/> " );
Connection =   New Sqlconnection (configurationmanager. connectionstrings [ " Northwinddb " ]. Connectionstring );
Sqlcommand. commandtext =   " Select companyName, contactname, city, postalcode from DBO. MERs " ;
Sqlcommand. commandtype = Commandtype. text;
Sqlcommand. Connection = Connection;
Connection. open ();
Datetime startdatetime = Datetime. now;
Asyncresult = Sqlcommand. beginexecutereader (commandbehavior. closeconnection );
While(! Asyncresult. iscompleted)
{
Response. Write ( " Retrieving information ...... <Br/> " );
Response. Flush ();
System. Threading. thread. Sleep ( 3000 );
}
Datareader = Sqlcommand. endexecutereader (asyncresult );
Datetime enddatetime = Datetime. now;
Timespan = Enddatetime. Subtract (startdatetime );
Response. Write ( " Information retrieval is complete. "   + Timespan. milliseconds +   " Millisecond " );
Response. Flush ();
Gvdata. datasource = Datareader;
Gvdata. databind ();

Connection. Close ();
}
}
}

Download Code: adoasyncdb.rar

 

ArticleSummary:

Asynchronous processing in ADO. Net-poll

Asynchronous processing in ADO. Net-Wait

Asynchronous processing in ADO. Net-function callback

 

 

 

 

 

 

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.