Asp.net| asynchronous (i). Simple introduction to the principle of implementation
The following illustration left is a procedure that does not use the asynchronous page feature (ASP.net 1.0), and the following illustration uses the asynchronous page execution process (asp.net 2.0).
(ASP.net 1.0 general processing process) (Asynchronous page feature processing using ASP.NET 2.0 new features)
From the left figure, one thread is always serving the request of the same page throughout the request process.
As can be seen from the right figure, in a page request process, can be different threads for this page request service.
Obviously, the use of the graph in the number of client requests, the overall efficiency of the site is high. Because:
1. When an asynchronous page is not used, a thread can only serve requests for the same page. This thread is still waiting even when the page request processes other I/O operations. When this page finishes using this thread, it is put back to the thread pool. The number of threads is limited! So when the thread is not used in time to put back to the line pool can be greatly improved system performance!
2. When using the asynchronous page feature, as in the right figure, the start Thread1 is for the page, but when the page handles other things (such as I/O or calls to other webservice), Thread1 is put back into the thread pool, at which point Thread1 can request services for other pages. When this page finishes its own operation, Thread2 then requests service for the page, not the original thread Thread1 used. This will improve the scalability of the site.
(b). Examples of Usage methods
I. Implement asynchronous page functionality with Page.addonprerendercompleteasync
A. Page Flag Plus properties: async= "true", add the following code:
<%@ Page language= "C #" autoeventwireup= "true"
Codefile= "AsyncPage.aspx.cs"
inherits= "_default" async= "true"%>
B. Background asynchronous page related code:
1private WebRequest _request;
2 protected void Page_Load (object sender, EventArgs e)
3 {
4//Register the Begin and end methods of an asynchronous call.
5 AddOnPreRenderCompleteAsync (
6 new BeginEventHandler (beginasyncoperation),
7 new EndEventHandler (endasyncoperation)
8);
9}
10
11//Asynchronous Call Start Method (when this method is executed, the current thread is returned to the thread pool, waiting to be serviced for another request).
IAsyncResult Beginasyncoperation (object sender, EventArgs E, AsyncCallback cb, Object State)
13 {
_request = WebRequest.Create ("http://blog.csdn.net/chengking/");
Return _request. BeginGetResponse (CB, State);
16}
17
18//The Receive method after the end of an asynchronous call (after the execution of the asynchronous operation completes, it will again fetch a thread from the thread pool to request service for this page).
void Endasyncoperation (IAsyncResult ar)
20 {
string text;
The using (WebResponse response = _request. EndGetResponse (AR))
23 {
using (StreamReader reader = new StreamReader (response). GetResponseStream ()))
25 {
-Text = reader. ReadToEnd ();
27}
28}
This.lbOupput.Text = Text;
30}
2. The database object SqlCommand implements the asynchronous invocation function.
A. Page Flag Plus properties: async= "true", add the following code:
1public Partial class AsyncVisitDatabase:System.Web.UI.Page
2{
3//Define Data manipulation objects
4 Private SqlConnection _connection;
5 Private SqlCommand _command;
6 Private SqlDataReader _reader;
7
8 protected void Page_Load (object sender, EventArgs e)
9 {
Ten if (! IsPostBack)
11 {
12//Registration Event Page_PreRender execution method when execution completes
this. Prerendercomplete + = new EventHandler (page_prerendercomplete);
14
15/**////registers the begin and end methods of an asynchronous call.
AddOnPreRenderCompleteAsync (
The new BeginEventHandler (beginasyncoperation),
New EndEventHandler (Endasyncoperation)
19);
20}
21}
22
23//Asynchronous Call Start Method (when this method is executed, the current thread is returned to the thread pool, waiting to be serviced for another request).
IAsyncResult Beginasyncoperation (object sender, EventArgs E, AsyncCallback cb, Object State)
25 {
String connect = webconfigurationmanager.connectionstrings["ConnectionString"]. ConnectionString;
_connection = new SqlConnection (connect);
_connection. Open ();
_command = new SqlCommand ("SELECT * from Sales", _connection);
Return _command. Beginexecutereader (CB, State);
31}
32
33//The Receive method after the end of an asynchronous call (after the execution of the asynchronous operation completes, it will again fetch a thread from the thread pool to request service for this page).
void Endasyncoperation (IAsyncResult ar)
35 {
_reader = _command. Endexecutereader (AR);
37}
38
39//Event Page_PreRender executes the method when execution completes, where an asynchronous call returns results to the controls on the page or to other aftermath operations.
protected void Page_prerendercomplete (object sender, EventArgs e)
41 {
Gridview1.datasource = _reader;
Gridview1.databind ();
44}
45
override void Dispose ()
47 {
if (_connection!= null)
_connection. Close ();
Base. Dispose ();
51}
52}
3. Implement asynchronous Call WebService
A. Page Flag Plus properties: async= "true", add the following code:
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.