PageAsyncTask 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
包含有关注册到页的异步任务的信息。 此类不能被继承。
public ref class PageAsyncTask sealed
public sealed class PageAsyncTask
type PageAsyncTask = class
Public NotInheritable Class PageAsyncTask
- 继承
-
PageAsyncTask
示例
下面的代码示例将三个异步任务注册到一个页面,并并行执行它们。 每个任务调用一个方法,该方法仅导致线程休眠 5 秒。
<%@ Page Language="C#" Async="true" AsyncTimeout="35"%>
<!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.SlowTask slowTask1 =
new Samples.AspNet.CS.Controls.SlowTask();
Samples.AspNet.CS.Controls.SlowTask slowTask2 =
new Samples.AspNet.CS.Controls.SlowTask();
Samples.AspNet.CS.Controls.SlowTask slowTask3 =
new Samples.AspNet.CS.Controls.SlowTask();
// <Snippet3>
PageAsyncTask asyncTask1 = new PageAsyncTask(slowTask1.OnBegin, slowTask1.OnEnd, slowTask1.OnTimeout, "Async1", true);
PageAsyncTask asyncTask2 = new PageAsyncTask(slowTask2.OnBegin, slowTask2.OnEnd, slowTask2.OnTimeout, "Async2", true);
PageAsyncTask asyncTask3 = new PageAsyncTask(slowTask3.OnBegin, slowTask3.OnEnd, slowTask3.OnTimeout, "Async3", true);
// Register the asynchronous task.
Page.RegisterAsyncTask(asyncTask1);
Page.RegisterAsyncTask(asyncTask2);
Page.RegisterAsyncTask(asyncTask3);
// </Snippet3>
// Execute the register asynchronous task.
Page.ExecuteRegisteredAsyncTasks();
TaskMessage.InnerHtml = slowTask1.GetAsyncTaskProgress()+ "<br />" + slowTask2.GetAsyncTaskProgress() + "<br />" + slowTask3.GetAsyncTaskProgress();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" 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" %>
<!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()
' <Snippet3>
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)
' </Snippet3>
' 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>
using System;
using System.Web;
using System.Web.UI;
using System.Threading;
namespace Samples.AspNet.CS.Controls
{
public class SlowTask
{
private String _taskprogress;
private AsyncTaskDelegate _dlgt;
// Create delegate.
protected delegate void AsyncTaskDelegate();
public String GetAsyncTaskProgress()
{
return _taskprogress;
}
public void ExecuteAsyncTask()
{
// Introduce an artificial delay to simulate a delayed
// asynchronous task.
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 = "AsyncTask started at: " + DateTime.Now + ". ";
_dlgt = new AsyncTaskDelegate(ExecuteAsyncTask);
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 += "AsyncTask completed at: " + DateTime.Now;
_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 += "AsyncTask failed to complete " +
"because it exceeded the AsyncTimeout parameter.";
}
}
}
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
注解
ASP.NET 版本 2.0 允许向页面注册多个任务,并在呈现页面之前异步运行这些任务。 如果任务是一个缓慢的进程,并且不希望在执行任务时绑定其他进程,则可以指定该任务以异步方式运行。 异步任务可以并行执行,也可以按顺序执行。
PageAsyncTask必须通过 RegisterAsyncTask 方法将 对象注册到页面。 无需异步处理页面本身就可以执行异步任务。 可以将 属性设置为 Async
true
(,如以下代码示例) 所示,或 false
页面指令上所示,异步任务仍会异步处理:
<%@ Page Async="true" %>
当 属性 Async
设置为 false
时,将阻止执行页面的线程,直到完成所有异步任务。
事件之前 PreRenderComplete 注册的任何异步任务都将由页面自动执行(如果尚未执行)。 必须在 事件后注册的 PreRenderComplete 异步任务必须通过 ExecuteRegisteredAsyncTasks 方法显式执行。 方法 ExecuteRegisteredAsyncTasks 还可用于在事件之前 PreRenderComplete 启动任务。 方法 ExecuteRegisteredAsyncTasks 执行页面上所有已注册的异步任务,但尚未执行。
默认情况下,如果异步任务尚未在 45 秒内完成,则会超时。 可以在 Web.config 文件或 page 指令中指定不同的超时值。
<pages>
Web.config 文件的 节包含属性asyncTimeout
,如下所示。
<system.web>
<pages asyncTimeout="30">
</pages>
</system.web>
page 指令包含特性 AsyncTimeout
。
<%@ Page AsyncTimeout="30" %>
构造函数
PageAsyncTask(BeginEventHandler, EndEventHandler, EndEventHandler, Object) |
使用并行执行的默认值初始化 PageAsyncTask 类的新实例。 |
PageAsyncTask(BeginEventHandler, EndEventHandler, EndEventHandler, Object, Boolean) |
使用并行执行的指定值初始化 PageAsyncTask 类的新实例。 |
PageAsyncTask(Func<CancellationToken,Task>) |
使用能取消任务的事件处理程序初始化 PageAsyncTask 类的新实例。 |
PageAsyncTask(Func<Task>) |
使用能处理任务的事件处理程序初始化 PageAsyncTask 类的新实例。 |
属性
BeginHandler |
获取当异步任务开始时要调用的方法。 |
EndHandler |
获取当任务在超时期内成功完成时要调用的方法。 |
ExecuteInParallel |
获取一个值,该值指示任务能否与其他任务并行处理。 |
State |
获取表示任务状态的对象。 |
TimeoutHandler |
获取当任务未在超时期内成功完成时要调用的方法。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |