A little hint of implementing multitask asynchronous page in asp.net2.0

Source: Internet
Author: User
Asp.net| Asynchronous

If you want to learn asp.net2.0 asynchronous page technology, then be sure to read the http://www.microsoft.com/china/MSDN/library/default.mspx?mfr=true carefully and download its source code carefully. This paper introduces 3 programming models to implement asynchronous pages, and the function is more powerful than one. I don't have much to say, just look at the last model: Using PageAsyncTask class, RegisterAsyncTask method, The Executeregisteredasynctasks method and the Timeoutasyncoperation method register and perform the asynchronous task and call the timeout handling method for a long, unresponsive situation. The original source AsyncPageTask.aspx.cs file provides a detailed example of how to implement an asynchronous page using this model.
The best thing about this model is that you can handle multiple asynchronous tasks in a single page request, and you can also use time-out to avoid unresponsive situations that occur when an asynchronous operation is performed. The original author only registers an asynchronous task in Page_Load, then we follow his example and register an asynchronous task as follows:

<%@ 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 you get the results returned by the asynchronous task within the specified time of page property asynctimeout, the page will appear as we expect. But what if you had a little trouble doing your first task and delayed your time? There are two possibilities: one is that the results of the first task are eventually returned and displayed, and the second task is determined to timeout as soon as it is started, thus executing its Timeoutasyncoperation method; second, the first task has been timed out without waiting for the return result. Therefore, the second task must also be sentenced to timeout. The above situation is because two asynchronous tasks share the asynctimeout time, as long as the previous task in the execution of delay time, will inevitably affect the subsequent task of the operation. So is it possible to have two asynchronous missions enjoy the asynctimeout time, which is to find a way out of the executeregisteredasynctasks approach.
It is noteworthy that asp.net2.0 resets the AsyncTimeout property each time the executeregisteredasynctasks is invoked, which means it is possible to achieve the exclusive asynctimeout time of the asynchronous task. According to the current program, if the call Executeregisteredasynctasks method is not displayed, asp.net2.0 will be in the page life cycle Prerendercomplete
The Executeregisteredasynctasks method is called automatically before the event to run the two registered asynchronous tasks. Because only one executeregisteredasynctasks was executed and two tasks were run, then the two tasks had to share the asynctimeout-mandated run time. 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: Does the second Executeregisteredasynctasks method execute the first asynchronous task that is registered again? Not really, because asp.net2.0 has already stipulated that the same asynchronous method will only execute once. So this makes two asynchronous tasks enjoy a unique run time, avoiding interference.



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.