PageAsyncTask Class
Collapse the table of content
Expand the table of content

PageAsyncTask Class


Contains information about an asynchronous task registered to a page. This class cannot be inherited.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)


public sealed class PageAsyncTask

System_CAPS_pubmethodPageAsyncTask(BeginEventHandler, EndEventHandler, EndEventHandler, Object)

Initializes a new instance of the PageAsyncTask class using the default value for executing in parallel.

System_CAPS_pubmethodPageAsyncTask(BeginEventHandler, EndEventHandler, EndEventHandler, Object, Boolean)

Initializes a new instance of the PageAsyncTask class using the specified value for executing in parallel.

System_CAPS_pubmethodPageAsyncTask(Func<CancellationToken, Task>)

Initializes a new instance of the PageAsyncTask class using an event handler that enables the task to be canceled.


Initializes a new instance of the PageAsyncTask class using an event handler that enables the task to be handled.


Gets the method to call when beginning an asynchronous task.


Gets the method to call when the task completes successfully within the time-out period.


Gets a value that indicates whether the task can be processed in parallel with other tasks.


Gets an object that represents the state of the task.


Gets the method to call when the task does not complete successfully within the time-out period.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

ASP.NET version 2.0 allows you to register multiple tasks to a page and run them asynchronously prior to rendering the page. You might specify that a task be run asynchronously if it is a slow process and you do not want other processes to be tied up while it is executing. The asynchronous tasks can be executed in parallel or sequentially.

A PageAsyncTask object must be registered to the page through the RegisterAsyncTask method. The page itself does not have to be processed asynchronously to execute asynchronous tasks. You can set the Async attribute to either true (as shown in the following code example) or false on the page directive and the asynchronous tasks will still be processed asynchronously:

<%@ Page Async="true" %>

When the Async attribute is set to false, the thread that executes the page will be blocked until all asynchronous tasks are complete.

Any asynchronous tasks registered before the PreRenderComplete event will be executed automatically by the page if they have not already been executed. Those asynchronous tasks registered after the PreRenderComplete event must be executed explicitly through the ExecuteRegisteredAsyncTasks method. The ExecuteRegisteredAsyncTasks method can also be used to start tasks before the PreRenderComplete event. The ExecuteRegisteredAsyncTasks method executes all the registered asynchronous tasks on the page that have not been executed.

By default, an asynchronous task will time out if it has not completed within 45 seconds. You can specify a different time-out value in either the Web.config file or the page directive. The <pages> section of the Web.config file contains an asyncTimeout attribute, as shown below.


<pages asyncTimeout="30">



The page directive contains an AsyncTimeout attribute.

<%@ Page AsyncTimeout="30" %>

The following code example registers three asynchronous tasks to a page and executes them in parallel. Each task calls a method that merely causes the thread to sleep for 5 seconds.

<%@ Page Language="C#" Async="true" AsyncTimeout="35"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  protected void Page_Load(object sender, EventArgs e)

    // Define the asynchronuous task.
    Samples.AspNet.CS.Controls.SlowTask slowTask1 =    
      new Samples.AspNet.CS.Controls.SlowTask();
    Samples.AspNet.CS.Controls.SlowTask slowTask2 =
    new Samples.AspNet.CS.Controls.SlowTask();
    Samples.AspNet.CS.Controls.SlowTask slowTask3 =
    new Samples.AspNet.CS.Controls.SlowTask();

    // <Snippet3> 
    PageAsyncTask asyncTask1 = new PageAsyncTask(slowTask1.OnBegin, slowTask1.OnEnd, slowTask1.OnTimeout, "Async1", true);
    PageAsyncTask asyncTask2 = new PageAsyncTask(slowTask2.OnBegin, slowTask2.OnEnd, slowTask2.OnTimeout, "Async2", true);
    PageAsyncTask asyncTask3 = new PageAsyncTask(slowTask3.OnBegin, slowTask3.OnEnd, slowTask3.OnTimeout, "Async3", true);

    // Register the asynchronous task.
    // </Snippet3>

    // Execute the register asynchronous task.

    TaskMessage.InnerHtml = slowTask1.GetAsyncTaskProgress()+ "<br />" + slowTask2.GetAsyncTaskProgress() + "<br />" + slowTask3.GetAsyncTaskProgress();


<html xmlns="" >
<head id="Head1" runat="server">
    <title>Asynchronous Task Example</title>
    <form id="form1" runat="server">
      <span id="TaskMessage" runat="server">
using System;
using System.Web;
using System.Web.UI;
using System.Threading;

namespace Samples.AspNet.CS.Controls
    public class SlowTask
        private String _taskprogress;
        private AsyncTaskDelegate _dlgt;

        // Create delegate.
        protected delegate void AsyncTaskDelegate();

        public String GetAsyncTaskProgress()
            return _taskprogress;
        public void ExecuteAsyncTask()
            // Introduce an artificial delay to simulate a delayed 
            // asynchronous task.

        // Define the method that will get called to
        // start the asynchronous task.
        public IAsyncResult OnBegin(object sender, EventArgs e,
            AsyncCallback cb, object extraData)
            _taskprogress = "AsyncTask started at: " + DateTime.Now + ". ";

            _dlgt = new AsyncTaskDelegate(ExecuteAsyncTask);
            IAsyncResult result = _dlgt.BeginInvoke(cb, extraData);

            return result;

        // Define the method that will get called when
        // the asynchronous task is ended.
        public void OnEnd(IAsyncResult ar)
            _taskprogress += "AsyncTask completed at: " + DateTime.Now;

        // Define the method that will get called if the task
        // is not completed within the asynchronous timeout interval.
        public void OnTimeout(IAsyncResult ar)
            _taskprogress += "AsyncTask failed to complete " +
                "because it exceeded the AsyncTimeout parameter.";

.NET Framework
Available since 2.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft