If you want to learn the asynchronous page technology of Asp.Net2.0, you must carefully read http://www.microsoft.com/china/MSDN/library/default.mspx?mfr=true , and download its source code and study it carefully. . The full text introduces a total of 3 programming models for implementing asynchronous pages, and each has more powerful functions than the other. I won’t say much more, let’s just look at the last model: use the PageAsyncTask class, RegisterAsyncTask method, ExecuteRegisteredAsyncTasks method and TimeoutAsyncOperation method to register and execute asynchronous tasks, and call the timeout processing method for long periods of no response. The AsyncPageTask.aspx.cs file of the source code provided in the original article provides a detailed example of how to use this model to implement asynchronous pages.
The biggest advantage of this model is that it can handle multiple asynchronous tasks in one page request, and timeout processing can also be used to avoid unresponsiveness when performing asynchronous operations. The original author only registered an asynchronous task in Page_Load, so we follow his example and register another asynchronous task, as shown below:
<%@ Page Async="true" AsyncTimeout="5" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="AsyncPageTask.aspx.cs" Inherits="AsyncPageTask" Title=" Untitled Page" %>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PageAsyncTask task = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation),
new EndEventHandler(TimeoutAsyncOperation),
null
);
PageAsyncTask task1 = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation1),
new EndEventHandler(EndAsyncOperation1),
new EndEventHandler(TimeoutAsyncOperation1),
null
);
RegisterAsyncTask(task);
RegisterAsyncTask(task1);
}
}
If the result returned by the asynchronous task is obtained within the specified time of the page attribute AsyncTimeout, the page will be displayed as we expected. But what happens if you run into a little trouble while performing your first task and lose time? There are two possibilities: first, the result of the first task is finally returned and displayed, and the second task is judged to have timed out as soon as it is started, thus executing its TimeoutAsyncOperation method; second, the first task does not It has been sentenced to timeout by the time the result is returned, so the second task must also have been sentenced to timeout. The above situation is due to the two asynchronous tasks sharing the time specified by AsyncTimeout. As long as the previous task is delayed during execution, it will inevitably affect the operation of the subsequent task. So can two asynchronous tasks have exclusive access to the time specified by AsyncTimeout? This requires finding a way through the ExecuteRegisteredAsyncTasks method.
It is worth noting that Asp.net2.0 will reset the AsyncTimeout property every time ExecuteRegisteredAsyncTasks is called, which means that it is possible to achieve an asynchronous task exclusive for the time specified by AsyncTimeout. According to the current way of writing the program, if the ExecuteRegisteredAsyncTasks method is not explicitly called, Asp.net2.0 will call PreRenderComplete in the page life cycle.
The ExecuteRegisteredAsyncTasks method is automatically called before the event to run the two registered asynchronous tasks. Because ExecuteRegisteredAsyncTasks is only executed once but two tasks are run, the two tasks have to share the running time specified by AsyncTimeout. So I made the following adjustments to the code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PageAsyncTask task = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation),
new EndEventHandler(TimeoutAsyncOperation),
null
);
PageAsyncTask task1 = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation1),
new EndEventHandler(EndAsyncOperation1),
new EndEventHandler(TimeoutAsyncOperation1),
null
);
RegisterAsyncTask(task);
ExecuteRegisteredAsyncTasks();
RegisterAsyncTask(task1);
ExecuteRegisteredAsyncTasks();
}
}
At first glance, there seems to be a problem: Will the second ExecuteRegisteredAsyncTasks method execute the first registered asynchronous task again? Actually no, because Asp.net2.0 has stipulated that the same asynchronous method will only be executed once. Therefore, the two asynchronous tasks have exclusive running time and avoid mutual interference.
http://www.cnblogs.com/taewind/archive/2006/12/22/600687.html