Page.ExecuteRegisteredAsyncTasks 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
开始执行异步任务。
public:
void ExecuteRegisteredAsyncTasks();
public void ExecuteRegisteredAsyncTasks ();
member this.ExecuteRegisteredAsyncTasks : unit -> unit
Public Sub ExecuteRegisteredAsyncTasks ()
例外
异步任务中有异常。
示例
下面的代码示例演示如何将 属性与 ExecuteRegisteredAsyncTasks 和 RegisterAsyncTask 方法结合使用AsyncTimeout。 请注意,使用开始、结束和超时处理程序。 在此示例中,引入了人工延迟来演示异步任务超出 属性中指定的 AsyncTimeout 任务分配时间的情况。 例如,在实际方案中,异步任务可用于执行数据库调用或映像生成,如果任务未在指定时间内执行,则超时处理程序提供正常降级。
<%@ 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>
<%@ Page Language="VB" 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 Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Define the asynchronuous task.
Dim mytask As New Samples.AspNet.VB.Controls.MyAsyncTask()
Dim asynctask As New PageAsyncTask(AddressOf mytask.OnBegin, AddressOf mytask.OnEnd, AddressOf mytask.OnTimeout, DBNull.Value)
' Register the asynchronous task.
Page.RegisterAsyncTask(asynctask)
' Execute the register asynchronous task.
Page.ExecuteRegisteredAsyncTasks()
TaskMessage.InnerHtml = mytask.GetAsyncTaskProgress()
End Sub
</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.";
}
}
}
Imports System.Web
Imports System.Web.UI
Imports System.Threading
Namespace Samples.AspNet.VB.Controls
Public Class MyAsyncTask
Private _taskprogress As String
Private _dlgt As AsyncTaskDelegate
' Create delegate.
Delegate Function AsyncTaskDelegate()
Public Function GetAsyncTaskProgress() As String
Return _taskprogress
End Function
Public Function DoTheAsyncTask()
' Introduce an artificial delay to simulate a delayed
' asynchronous task. Make this greater than the
' AsyncTimeout property.
Thread.Sleep(TimeSpan.FromSeconds(5.0))
End Function
' 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 = "Beginning async task."
Dim _dlgt As New AsyncTaskDelegate(AddressOf DoTheAsyncTask)
Dim result As IAsyncResult = _dlgt.BeginInvoke(cb, extraData)
Return result
End Function 'OnBegin
' Define the method that will get called when
' the asynchronous task is ended.
Public Sub OnEnd(ByVal ar As IAsyncResult)
_taskprogress = "Asynchronous task completed."
_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 = "Ansynchronous task failed to complete because " & _
"it exceeded the AsyncTimeout parameter."
End Sub
End Class
End Namespace
注解
使用 PageAsyncTask 类定义异步任务。 定义任务并使用 方法向页面 RegisterAsyncTask 注册后, ExecuteRegisteredAsyncTasks 可以调用 方法来开始异步任务。
ExecuteRegisteredAsyncTasks当为非异步页面调用任何已注册的异步任务(如果存在)时,将在页面处理时自动调用 方法。 对 的自动调用 ExecuteRegisteredAsyncTasks 恰好发生在事件之前 PreRenderComplete 。 ExecuteRegisteredAsyncTasks为要在自动调用此方法以外的时间调用的任务调用 方法。 请注意,异步任务将只执行一次,即使 ExecuteRegisteredAsyncTasks 可以多次调用。
每次调用 ExecuteRegisteredAsyncTasks 方法时,都会AsyncTimeout重置 属性。 在调用 ExecuteRegisteredAsyncTasks 方法之前的最后一个值AsyncTimeout优先。 如果异步任务所花费的时间超过 , AsyncTimeout则在该 ExecuteRegisteredAsyncTasks 调用期间调用的后续任务会立即超时。