Asynchronous ASP.NET ASPX Pages

When you take advantage of asynchronous ADO.NET methods, you must also enable asynchronous ASP.NET page execution. You enable an asynchronous ASP.NET page by adding the following two attributes to a page directive:

<%@ Page Async=”true” AsyncTimeout=”8” %>

The first attribute enables asynchronous page execution. The second attribute specifies a timeout value in seconds. The timeout value specifies the amount of time that the page gives a set of asynchronous tasks to complete before the page continues execution.
After you enable asynchronous page execution, you must set up the asychronous tasks and register the tasks with the page. You represent each asynchronous task with an instance of the PageAsyncTask object. You register an asynchronous task for a page by calling the Page.RegisterAsyncTask() method.

<%@ Page Language=”C#” Async=”true” AsyncTimeout=”1” Trace=”true” %>
<%@ Import Namespace=”System.Threading” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
private AsyncDataLayer dataLayer = new AsyncDataLayer();
void Page_Load()
{
// Setup asynchronous data execution
PageAsyncTask task = new PageAsyncTask(BeginGetData, EndGetData,TimeoutData, null, true);
Page.RegisterAsyncTask(task);
// Fire off asynchronous tasks
Page.ExecuteRegisteredAsyncTasks();
}
IAsyncResult BeginGetData(object sender, EventArgs e, AsyncCallback callback,object state)
{
// Show Page Thread ID
Trace.Warn(“BeginGetData: “ + Thread.CurrentThread.GetHashCode());
// Execute asynchronous command
return dataLayer.BeginGetMovies(callback, state);
}
void EndGetData(IAsyncResult ar)
{
// Show Page Thread ID

Executing Asynchronous Database Commands
Trace.Warn(“EndGetDate: “ + Thread.CurrentThread.GetHashCode());
// Bind results
GridShowValue.DataSource = dataLayer.EndGetMovies(ar);
GridShowValue.DataBind();
}
void TimeoutData(IAsyncResult ar)
{
// Display error message
lblError.Text = “Could not retrieve data!”;
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Show Page AsyncTask</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:Label id=”lblError” Runat=”server” />
<asp:GridView id=”GridShowValue” Runat=”server” />
</div>
</form>
</body>
</html>
The PageAsyncTask object that represents the asynchronous task. Next, the PageAsyncTask object is registered for the page with the Page.RegisterAsyncTask() method. Finally, a call to the Page.
ExecuteRegisteredAsyncTasks() method executes the task. (If you don’t call this method, any asynchronous tasks registered for the page are executed during the PreRender event automatically.)

The constructor for the PageAsyncTask object accepts the following parameters:
  • beginHandler : The method that executes when the asynchronous task begins.
  • endHandler : The method that executes when the asynchronous task ends.
  • timoutHandler : The method that executes when the asynchronous task runs out of time according to the Page directive’s AsyncTimeout attribute.
  • state : An arbitrary object that represents state information.
  • executeInParallel : A Boolean value that indicates whether multiple asynchronous tasks should execute at the same time or execute in sequence.
You can create multiple PageAsyncTask objects and register them for the same page. When you call the ExecuteRegisteredAsyncTasks() method, all the registered tasks are executed.
If an asynchronous task does not complete within the time alloted by the AsyncTimeout attribute, then the timoutHandler method executes.
e.g, the page asychronous tasks 5 seconds to execute. If the database SELECT command does not return a record within the 5 seconds, then the TimeoutData() method executes.

It is important to understand that the asynchronous task continues to execute even whenthe task executes longer than the interval of time specified by the AsyncTimeout attribute.The AsyncTimeout attribute specifies the amount of time that a page is willing to wait before continuing execution. An asynchronous task is not canceled if takes too long.
Tags: , ,
Hot on Web:


About author