Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Page.ExecuteRegisteredAsyncTasks Method

Starts the execution of an asynchronous task.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
public void ExecuteRegisteredAsyncTasks()
ExceptionCondition
HttpException

There is an exception in the asynchronous task.

Define an asynchronous task using the PageAsyncTask class. After the task is defined, and is registered with the page using the RegisterAsyncTask method, the ExecuteRegisteredAsyncTasks method can be invoked to begin the asynchronous task.

The ExecuteRegisteredAsyncTasks method is automatically called at the point in the page processing when any registered asynchronous tasks, if they exist, are invoked for a non-asynchronous page. This automatic call to ExecuteRegisteredAsyncTasks occurs just before the PreRenderComplete event. Call the ExecuteRegisteredAsyncTasks method for tasks you want to be invoked at times other than the automatic call to this method. Note, asynchronous tasks will be executed only once even though ExecuteRegisteredAsyncTasks may be called more than once.

The AsyncTimeout property is reset on every call to the ExecuteRegisteredAsyncTasks method. The last value of the AsyncTimeout prior to invoking the ExecuteRegisteredAsyncTasks method takes precedence. If an asynchronous task takes more than the AsyncTimeout, subsequent tasks invoked during that ExecuteRegisteredAsyncTasks call are timed out immediately.

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.

<%@ Page Language="C#" AsyncTimeout="2"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    // Define the asynchronuous task.
    Samples.AspNet.CS.Controls.MyAsyncTask mytask =    
      new Samples.AspNet.CS.Controls.MyAsyncTask();
    PageAsyncTask asynctask = new PageAsyncTask(mytask.OnBegin, mytask.OnEnd, mytask.OnTimeout, null);

    // Register the asynchronous task.
    Page.RegisterAsyncTask(asynctask);

    // Execute the register asynchronous task.
    Page.ExecuteRegisteredAsyncTasks();

    TaskMessage.InnerHtml = mytask.GetAsyncTaskProgress();

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Asynchronous Task Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <span id="TaskMessage" runat="server">
      </span>
    </div>
    </form>
</body>
</html>
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.
			Thread.Sleep(TimeSpan.FromSeconds(5.0));
		}

		// 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.";
			_dlgt.EndInvoke(ar);
		}

		// 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

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.