Here: http://msdn.microsoft.com/en-us/magazine/cc163725.aspx
ASP. net2.0 asynchronous page technology is used to solve the long-running page time, and the pages caused by complicated logic Code are not displayed for a long time. It is quite different from Ajax.
ASP. net2.0 asynchronous technology adopts. NET Framework multithreading technology. When we are in ASP. when async = 'true' is marked on the net page, the page compiler will automatically compile it into an asynchronous class and implement ihttpasynchandler. at the same time, aspnet_isapi will bring the request to the asynchronous pipeline for actual page execution.
Now we find that after the page_prerender event and before the prerendercomplete event, it starts a background thread to execute the corresponding task. Why does it happen in this event, and how does the background thread find and return results? That's because there is something called async point "Asynchronous point.
Public partial class mypages: system. Web. UI. Page
{
Private const string rssfeed = "http://weblogs.asp.net/despos/rss.aspx ";
Private webrequest weQ;
Protected void page_load (Object sender, eventargs E)
{
Addonprerendercompleteasync (New begineventhandler (beginresult ),
New endeventhandler (endtask ));
}
Iasyncresult beginresult (Object SRC, eventargs ARGs, asynccallback CB, object state)
{
Trace. Warn ("custom message", "begin async: thread =" + thread. currentthread. managedthreadid. tostring ());
WeQ = webrequest. Create (rssfeed );
Return weQ. begingetresponse (CB, State );
}
Void endtask (iasyncresult AR)
{
String text;
Using (webresponse response = weQ. endgetresponse (AR ))
{
Streamreader reader;
Using (reader = new streamreader (response. getresponsestream ()))
{
TEXT = reader. readtoend ();
}
Literal1.text = text;
}
}
}
But what should we do if we want to process multiple asynchronous tasks at the same time? Usually
Void page_load (Object sender, eventargs E)
{
Addonprerendercompleteasync (
New begineventhandler (begintask ),
New endeventhandler (endtask ));
Addonprerendercompleteasync (
New begineventhandler (begintask1 ),
New endeventhandler (endtask1 ));
Addonprerendercompleteasync (
New begineventhandler (begintask1 ),
New endeventhandler (endtask1 ));
}
However, when we start the trace, we will find that:
Two threads are started, and only all requests are returned for the asynchronous page. This means that if we write n asynchronous calls, we will start n asynchronous threads, and if one of them gets stuck, the page will be blocked until it times out.
Of course, that is not good. In fact, in the face of such problems, we all need to customize iasyncresult. Internally,We can control whether all tasks are completed before returning. For specific implementation, please download the code to view(Compositeasyncresult. CS)
When we execute the following code, we will find that only one thread is started.
Void page_load (Object sender, eventargs E)
{
Addonprerendercompleteasync (
New begineventhandler (begintask ),
New endeventhandler (endtask ));
}
Iasyncresult begintask (Object sender, eventargs E, asynccallback CB, object state)
{
// Create the custom asyncresult object
Compositeasyncresult AR = new compositeasyncresult (CB, state, 2 );
// Fire the first request
Req1 = webrequest. Create (rssfeed1 );
AR1 = req1.begingetresponse (AR. Callback, State );
// Fire the second request
Req2 = webrequest. Create (rssfeed2 );
AR2 = req2.begingetresponse (AR. Callback, State );
Return Ar;
}
At the same time, I would like to mention the pageasynctask method:
Looking at the explanations on msdn, we will find that it is an independent asynchronous page. Even if you set the page's async = 'false', it implements asynchronous tasks in the same way, but it will block the page thread.
ASP. NET 2.0 allows you to register multiple tasks on the page and run these tasks asynchronously before the page is displayed. If the task process is slow and you do not want to delay other processes when running it, you can specify to run the task asynchronously. Asynchronous tasks can be executed in parallel or in sequence.
PageasynctaskThe object must be registered to the page through the registerasynctask method. The page itself does not require asynchronous processing to execute asynchronous tasks. You can addAsyncSet propertyTrue(As shown in the following code example) orFalse, The asynchronous task will still be processed asynchronously:
<% @ Page async = "true" %>
WhenAsyncSet propertyFalseBefore all asynchronous tasks are completed, the thread on the execution page will be blocked.
If an asynchronous task registered before the prerendercomplete event has not yet been executed, it will be automatically executed by the page. InPrerendercompleteThe asynchronous task registered after the event must be explicitly executed using the executeregisteredasynctasks method.ExecuteregisteredasynctasksThe method can also be used inPrerendercompleteStart the task before the event.ExecuteregisteredasynctasksMethod execution page.
By default, an asynchronous task times out if it is not completed within 45 seconds. You can specify different timeout values in the web. config file or page command. In the Web. config file<Pages>Section includesAsynctimeoutProperties, as shown below.
<System. Web>
<Pages asynctimeout = "30">
</Pages>
</System. Web>
Page instructions includeAsynctimeoutAttribute.
<% @ Page asynctimeout = "30" %>
Code with the same features:
Public partial class rssasync: system. Web. UI. Page
{
Private const string rssfeed1 = "http://weblogs.asp.net/despos/rss.aspx ";
Private const string rssfeed2 = "http://blogs.ugidotnet.org/dinoes/rss.aspx ";
Rssfeedasyncreader rss1, RSS2;
Public String rssdata;
Void page_load (Object sender, eventargs E)
{
This. prerendercomplete + = new eventhandler (rssasync_prerendercomplete );
Rss1 = new rssfeedasyncreader (rssfeed1 );
RSS2 = new rssfeedasyncreader (rssfeed2 );
Pageasynctask task1 = new pageasynctask (
New begineventhandler (rss1.beginread ),
New endeventhandler (rss1.endread ),
Null,
Null,
True );
Pageasynctask task2 = new pageasynctask (
New begineventhandler (rss2.beginread ),
New endeventhandler (rss2.endread ),
Null,
Null,
True );
Registerasynctask (task1 );
Registerasynctask (task2 );
}
Void rssasync_prerendercomplete (Object sender, eventargs E)
{
Rssdata = rss1.getrssdata () + rss2.getrssdata ();
}
}
Download
For more information about pageasynctask, click: http://msdn.microsoft.com/zh-cn/library/system.web.ui.pageasynctask (vs.80). aspx
Here, we will have some questions about which method (addonprerendercompleteasync or registerasynctask) is suitable? What are their differences?
Similarities: in terms of functions, they are the same, and page request execution is divided into two stages, before and after the asynchronous point (async point.
Differences:
1. registerasynctask is an API designed to run asynchronous tasks of the page class. It is not just an asynchronous page. Addonprerendercompleteasync is an API dedicated to executing asynchronous pages.
2. one is that registerasynctask executes the end handler on a thread with a richer context than addonprerenderccompleteasync. the thread context parameters des impersonation and httpcontext information that is missing in the thread serving the end handler of a classic asynchronous page. in addition, registerasyncasynctask allow you to set a timeout to ensure that any task doesn't run for more than a given number of seconds.
The other difference is that registerasynctask makes the implementation of multiple CALS to remote sources significantly easier. you can have parallel execution by simply setting a Boolean flag, and you don't need to create and manage your own iasyncresult object.
The bottom line is that you can use either approach for a single task, but you shoshould opt for registerasynctask when you have multiple task to execute simultancely.