PageAsyncTask.BeginHandler Property
.NET Framework (current version)
Gets the method to call when beginning an asynchronous task.
Assembly: System.Web (in System.Web.dll)
Property Value
Type: System.Web.BeginEventHandlerA BeginEventHandler delegate that represents the method to call when beginning the asynchronous task.
The BeginHandler delegate is set in the constructor.
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. A BeginHandler delegate is specified for each task.
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' Define the asynchronuous task. Dim slowTask1 As New Samples.AspNet.VB.Controls.SlowTask() Dim slowTask2 As New Samples.AspNet.VB.Controls.SlowTask() Dim slowTask3 As New Samples.AspNet.VB.Controls.SlowTask() Dim asyncTask1 As New PageAsyncTask(AddressOf slowTask1.OnBegin, AddressOf slowTask1.OnEnd, AddressOf slowTask1.OnTimeout, "Async1", True) Dim asyncTask2 As New PageAsyncTask(AddressOf slowTask2.OnBegin, AddressOf slowTask2.OnEnd, AddressOf slowTask2.OnTimeout, "Async2", True) Dim asyncTask3 As New PageAsyncTask(AddressOf slowTask3.OnBegin, AddressOf slowTask3.OnEnd, AddressOf slowTask3.OnTimeout, "Async3", True) ' Register the asynchronous task. Page.RegisterAsyncTask(asyncTask1) Page.RegisterAsyncTask(asyncTask2) Page.RegisterAsyncTask(asyncTask3) ' Execute the register asynchronous task. Page.ExecuteRegisteredAsyncTasks() TaskMessage.InnerHtml = slowTask1.GetAsyncTaskProgress() + "<br />" + slowTask2.GetAsyncTaskProgress() + "<br />" + slowTask3.GetAsyncTaskProgress() End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <span id="TaskMessage" runat="server"> </span> </div> </form> </body> </html>
Imports Microsoft.VisualBasic Imports System.Threading Namespace Samples.AspNet.VB.Controls Public Class SlowTask Private _taskprogress As String Private _dlgt As AsyncTaskDelegate ' Create delegate. Protected Delegate Sub AsyncTaskDelegate() Public Function GetAsyncTaskProgress() As String Return _taskprogress End Function Public Sub ExecuteAsyncTask() ' Introduce an artificial delay to simulate a delayed ' asynchronous task. Thread.Sleep(TimeSpan.FromSeconds(5.0)) End Sub ' Define the method that will get called to ' start the asynchronous task. Public Function OnBegin(ByVal sender As Object, ByVal e As EventArgs, ByVal cb As AsyncCallback, ByVal extraData As Object) As IAsyncResult _taskprogress = "AsyncTask started at: " + DateTime.Now.ToString + ". " _dlgt = New AsyncTaskDelegate(AddressOf ExecuteAsyncTask) Dim result As IAsyncResult = _dlgt.BeginInvoke(cb, extraData) Return result End Function ' Define the method that will get called when ' the asynchronous task is ended. Public Sub OnEnd(ByVal ar As IAsyncResult) _taskprogress += "AsyncTask completed at: " + DateTime.Now.ToString _dlgt.EndInvoke(ar) End Sub ' Define the method that will get called if the task ' is not completed within the asynchronous timeout interval. Public Sub OnTimeout(ByVal ar As IAsyncResult) _taskprogress += "AsyncTask failed to complete " + _ "because it exceeded the AsyncTimeout parameter." End Sub End Class End Namespace
.NET Framework
Available since 2.0
Available since 2.0
Show: