To understand how to process Asp.net 2.0 asynchronous pages, first list the page lifecycle:
1: init event: Page initialization and initialization settings.
2: loadviewstate method: load the view State and fill in the viewstate attribute.
3: loadpostdata method: process the returned data and input form data.
4: Load event: load the page. The page control is initialized and reflects the client data.
5: raisepostdatachangedevent Method: A change event is triggered when a change notification is sent back.
6: raisepostbackevent method: process the send-back event, process the client events that cause the send-back, and raise the corresponding time on the service.
7: prerender event: Page pre-rendering.
8: saveviewstate method: Save the view State and save the viewstate attribute to the string.
9: render method: displays the page.
10: dispose method: determines whether to reference expensive resources.
11: Unload event: uninstall page.
Page processing method:
1: synchronous processing;
2: asynchronous processing.
Synchronous request process:
1: When ASP. NET receives a page request, it extracts a thread from the thread pool and assigns the request to the thread.
2: The page retains the thread during this request to prevent this thread from being used to process other requests.
3: If a synchronization request requires a long operation time, the thread allocated to the request is suspended before the return result is called.
4: Wait for the thread to return and complete other lifecycle of the page.
The relationship between the synchronization request lifecycle and the thread:
Synchronization request Problems:
The available threads in the thread pool are limited. If there are too many requests at this time, ASP. NET will fail subsequent requests due to the 503 "server unavailable" error. This greatly reduces the number of requests that Asp.net can receive and affects scalability.
Asynchronous processing process:
The preceding two points are the same as normal synchronous requests. The difference is the processing method for time-consuming processes:
1: After an asynchronous operation starts to respond to ASP. NET signals, the thread returns to the thread pool.
2: ASP. NET calls the begin method registered using addonprerendercompleteasync. The task of the begin method is to start asynchronous operations such as database queries or web service calls and return immediately.
3: The thread is returned to the thread pool. At the same time, the begin method returns iasyncresult.
4: ASP. NET extracts threads from the thread pool and calls the end method.
5: After the end is returned, ASP. NET executes the rest of the lifecycle of the page.
Relationship between the lifecycle and thread of an asynchronous request:
Advantages of Asynchronization:
Thread Pool threads are used efficiently, improving scalability. The thread that was originally suspended for waiting can now be used to serve other requests.
Example of Asynchronously loading data:
Step 1: Make the Page Support Asynchronization. Set the async attribute.
Step 2: configure the database connection string to Allow Asynchronous database operations.
Asynchronous Processing = true.
Step 3: register an asynchronous event in the page_load event on the page.
Protected Void Page_load ( Object Sender, eventargs E)
{
Addonprerendercompleteasync (
New Begineventhandler (beginasyncoperation ),
New Endeventhandler (endasyncoperation)
);
}
Iasyncresult beginasyncoperation ( Object Sender, eventargs E, asynccallback CB, Object State)
{
String SQL = " Select top 10 * From DBO. card_ext " ;
Sqlconnection _ Conn= NewSqlconnection (configurationmanager.
Appsettings ["Dataaccesscontionstringread"]. Tostring ());
_ Conn. open ();
Sqlcommand cmd= NewSqlcommand (SQL, _ conn );
Iasyncresult risynresult=Cmd. beginexecutereader (CB, CMD,
Commandbehavior. closeconnection );
Return Risynresult;
}
Void Endasyncoperation (iasyncresult iresult)
{
If ( ! Iresult. iscompleted)
{
Iresult. asyncwaithandle. waitone ();
}
Else
{
Sqldatareader Dr = (Iresult. asyncstate As Sqlcommand). endexecutereader (iresult );
If ( ! Dr. isclosed)
{
List < String > _ List = New List < String > ();
While (Dr. Read ())
{
_ List. Add (Dr [ 0 ]. Tostring ());
}
This . Gridview1.datasource = _ List;
This . Gridview1.databind ();
}
Dr. Close ();
}
}
Common asynchronous processing scenarios in. Net:
1: filestream, which is the I/O operation we often call:
2: socket;
3: sqlcommand, for example, beginexecutereader and beginexecutenonquery.
4: webrequest, such as crawling webpages;
5: webservcie call.
Notes for asynchronous calls:
The followingProgramIt is also legal. Although this method is asynchronous, it works the same as synchronous calling, because the endexecutereader method suspends the current thread until the result is returned. In terms of performance and resources, it is worse than directly using synchronization because the system needs to start one more thread. A better way is to use the callback method. For details, refer to the aboveCode.
Cmd. beginexecutereader (commandbehavior. closeconnection );
Cmd. endexecutereader (risynresult );
Differences between Asp.net asynchronous pages and Ajax asynchronous pages:
1: Asp.net's asynchronous page gave me the first idea to compare the results with Ajax. Ajax applications are time-consuming. When loading data, use a text prompt in the area where data is displayed, for example, loading data... no matter how time-consuming this method is, it only affects the data display of this Part, and other areas of the page are not affected (if the speed is not slow ). Asp.net asynchronous pages cannot achieve this effect. From the above asynchronous execution flowchart, we can see that the events of asynchronous calls occur in onprerendercomplete, that is, within the lifecycle of the page, to completely present the page to the user, you must wait for the Asynchronous Method to return the result. For example, if a time-consuming asynchronous Io method takes 10 seconds, you must view the page at least 10 seconds.
2: asynchronous pages increase the number of requests to be processed. The thread pool threads are used efficiently and the scalability is improved, ajax calls the callback function to perform DOM data loading based on the returned results after the page is loaded.
Asynchronous task:
ASP. NET 2.0 introduces another method to simplify asynchronous operations: registerasynctask has the following advantages over addonprerendercompleteasync.
1: Besides the begin and end methods, registerasynctask allows you to register the timeout method called when the asynchronous operation cannot be completed for a long time. Set the asynctimeout attribute in the @ page command to set timeout.
2: You can call registerasynctask multiple times in a request to register several asynchronous operations.
3: You can use the fourth parameter of registerasynctask to pass the status to the begin method. This parameter is generally set to null.
The asynchronous page of registerasynctask is similar to the asynchronous page dependent on addonprerendercompleteasync. You need to set the asyncmode attribute of this page to true and execute the event with the prerender. In this case, call the begin method registered with registerasynctask instead of addonprerendercompleteasync, the request is further processed until the last operation is completed.
Summary:
Correct use of Asp.net asynchronous pages can optimize application performance in some environments.
Note:
Reference:
1: http://www.cnblogs.com/JeffreyZhao/archive/2008/02/24/use-async-operation-properly.html
2: http://dev.yesky.com/msdn/189/2396189.shtml