RegisterAsyncTask Method
Collapse the table of content
Expand the table of content

Page.RegisterAsyncTask Method (PageAsyncTask)


Registers a new asynchronous task with the page.

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

public void RegisterAsyncTask(
	PageAsyncTask task


Type: System.Web.UI.PageAsyncTask

A PageAsyncTask that defines the asynchronous task.

Exception Condition

The asynchronous task is null.

Define an asynchronous task using the PageAsyncTask class. When the task is defined, use the RegisterAsyncTask method to register the task with the page. After registering the task, invoke the ExecuteRegisteredAsyncTasks method to begin the asynchronous task.

The RegisterAsyncTask method can be used with both synchronous and asynchronous pages.

The following code example demonstrates the use of the AsyncTimeout property with the ExecuteRegisteredAsyncTasks and RegisterAsyncTask methods. Note the use of beginning, ending, and time-out handlers. In the example, an artificial delay is introduced to demonstrate the situation of an asynchronous task exceeding the allotted time for the task as specified in the AsyncTimeout property. In a real-world scenario, an asynchronous task could be used to perform database calls or image generation, for example, and the time-out handler provides graceful degradation if the task is not performed in a specified amount of time.

using System;
using System.Web;
using System.Web.UI;
using System.Threading;

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

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

		public String GetAsyncTaskProgress()
			return _taskprogress;
		public void DoTheAsyncTask()
			// Introduce an artificial delay to simulate a delayed 
			// asynchronous task. Make this greater than the 
			// AsyncTimeout property.

		// Define the method that will get called to
		// start the asynchronous task.
		public IAsyncResult OnBegin(object sender, EventArgs e,
			AsyncCallback cb, object extraData)
			_taskprogress = "Beginning async task.";

			_dlgt = new AsyncTaskDelegate(DoTheAsyncTask);
			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 = "Asynchronous task completed.";

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

.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft