Tips for implementing multi-task asynchronous pages in ASP. net2.0

Source: Internet
Author: User

If you want to learn ASP. net2.0 asynchronous page technology, so be sure to carefully read the http://www.microsoft.com/china/MSDN/library/default.mspx? MFR = true, and download itsSource codeThink carefully. Three programming models for implementing asynchronous pages are introduced in the full text, and the functions are more powerful than the other. I will not talk much about it. Let's look at the last model: Register and execute asynchronous tasks using the pageasynctask class, registerasynctask method, executeregisteredasynctasks method, and timeoutasyncoperation method, and call the timeout Processing Method for long time without response. The asyncpagetask. aspx. CS file in the source code provided in the original article provides a detailed example of how to use this model to Implement Asynchronous pages.
The biggest benefit of this model is that it can process multiple asynchronous tasks in a single page request, and it can also use time-out processing to avoid non-response during asynchronous operations. The original author only registers an asynchronous task in page_load, so we will 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)
{< br> If (! Ispostback)
{< br> 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 property asynctimeout, the page will be displayed as expected. But what if I had a small trouble in executing the first task and delayed the time? There are two possibilities: one is that the result of the first task is finally returned and displayed, and the second task is regarded as timeout when it is started, thus executing its timeoutasyncoperation method; second, the first task has been sentenced to timeout without waiting for the returned result. Therefore, the second task must have been determined to have timed out. The above situation is because two asynchronous tasks share the time specified by asynctimeout, as long as the previous Task delays the time during execution, it will inevitably affect the running of the subsequent task. So can two asynchronous tasks exclusively enjoy the time specified by asynctimeout? It is necessary to find a way out in the executeregisteredasynctasks method.
It is worth noting that every time you call executeregisteredasynctasks, ASP. net2.0 resets the asynctimeout attribute, which means that asynchronous tasks may be able to exclusively enjoy the time specified by asynctimeout. According to the current Program If the executeregisteredasynctasks method is not displayed, ASP. net2.0 will
Before the event, the executeregisteredasynctasks method is automatically called to run the two registered asynchronous tasks. Because only one executeregisteredasynctasks executes two tasks, the two tasks have to share the running time specified by asynctimeout. So I Code Made the following adjustments:

protected void page_load (Object sender, eventargs e)
{< br> If (! Ispostback)
{< br> pageasynctask task = new pageasynctask (
New begineventhandler (beginasyncoperation),
New endeventhandler (endasyncoperation ),
New endeventhandler (timeoutasyncoperation),
null
);

pageasynctask task1 = new pageasynctask (
New begineventhandler (listener),
New endeventhandler (endasyncoperation1),
New endeventhandler (listener ),
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 already specifies that the same Asynchronous Method will only be executed once. Therefore, the two asynchronous tasks have exclusive running time to avoid mutual interference.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/JOHNCOOLS/articles/1467373.aspx

Related Article

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.