Page.ExecuteRegisteredAsyncTasks 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開始執行非同步工作。
public:
void ExecuteRegisteredAsyncTasks();
public void ExecuteRegisteredAsyncTasks ();
member this.ExecuteRegisteredAsyncTasks : unit -> unit
Public Sub ExecuteRegisteredAsyncTasks ()
例外狀況
非同步工作中發生例外狀況。
範例
下列程式碼範例示範搭配 和 RegisterAsyncTask 方法使用 AsyncTimeout 屬性 ExecuteRegisteredAsyncTasks 。 請注意,使用開始、結束和逾時處理常式。 在此範例中,引進了人工延遲,以示範非同步工作超過 屬性中所 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 可以多次呼叫,非同步工作仍只會執行一次。
每次 AsyncTimeout 呼叫 ExecuteRegisteredAsyncTasks 方法時都會重設 屬性。 叫用 方法之前 ExecuteRegisteredAsyncTasks 的最後一個值 AsyncTimeout 優先。 如果非同步工作花費超過 AsyncTimeout ,則該 ExecuteRegisteredAsyncTasks 呼叫期間叫用的後續工作會立即逾時。