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檔案或頁面指示詞中指定不同的逾時值。
<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) |