Page.ExecuteRegisteredAsyncTasks Page.ExecuteRegisteredAsyncTasks Page.ExecuteRegisteredAsyncTasks Page.ExecuteRegisteredAsyncTasks Method

定義

開始執行非同步工作。Starts the execution of an asynchronous task.

public:
 void ExecuteRegisteredAsyncTasks();
public void ExecuteRegisteredAsyncTasks ();
member this.ExecuteRegisteredAsyncTasks : unit -> unit
Public Sub ExecuteRegisteredAsyncTasks ()

例外狀況

非同步工作中發生例外狀況。There is an exception in the asynchronous task.

範例

下列程式碼範例示範如何使用AsyncTimeout具有屬性ExecuteRegisteredAsyncTasksRegisterAsyncTask方法。The following code example demonstrates the use of the AsyncTimeout property with the ExecuteRegisteredAsyncTasks and RegisterAsyncTask methods. 請注意,開始、 結束和逾時的處理常式使用。Note the use of beginning, ending, and time-out handlers. 在範例中,為了示範非同步工作超過配置的時間中指定工作的情況下引進假造的延遲AsyncTimeout屬性。In the example, an artificial delay is introduced to demonstrate the situation of an asynchronous task exceeding the allotted time for the task as specified in the AsyncTimeout property. 在真實世界案例中,非同步工作可用來執行資料庫呼叫或產生映像,比方說,而逾時的處理常式提供正常降級,如果工作未在指定的時間內執行。In a real-world scenario, an asynchronous task could be used to perform database calls or image generation, for example, and the time-out handler provides graceful degradation if the task is not performed in a specified amount of time.

<%@ 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 Microsoft.VisualBasic
Imports System
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類別。Define an asynchronous task using the PageAsyncTask class. 工作定義,且已向頁面使用之後RegisterAsyncTask方法,ExecuteRegisteredAsyncTasks可以叫用方法,開始非同步工作。After the task is defined, and is registered with the page using the RegisterAsyncTask method, the ExecuteRegisteredAsyncTasks method can be invoked to begin the asynchronous task.

ExecuteRegisteredAsyncTasks會自動呼叫方法中的點頁面上,處理任何註冊的非同步工作,如果它們存在時,會叫用非非同步頁面。The ExecuteRegisteredAsyncTasks method is automatically called at the point in the page processing when any registered asynchronous tasks, if they exist, are invoked for a non-asynchronous page. 這個自動呼叫ExecuteRegisteredAsyncTasks之前會發生PreRenderComplete事件。This automatic call to ExecuteRegisteredAsyncTasks occurs just before the PreRenderComplete event. 呼叫ExecuteRegisteredAsyncTasks方法您想要叫用有時自動呼叫此方法以外的工作。Call the ExecuteRegisteredAsyncTasks method for tasks you want to be invoked at times other than the automatic call to this method. 請注意,只有一次即使將會執行非同步工作ExecuteRegisteredAsyncTasks可能多次呼叫。Note, asynchronous tasks will be executed only once even though ExecuteRegisteredAsyncTasks may be called more than once.

AsyncTimeout屬性在每次呼叫重設ExecuteRegisteredAsyncTasks方法。The AsyncTimeout property is reset on every call to the ExecuteRegisteredAsyncTasks method. 最後一個值AsyncTimeout叫用之前ExecuteRegisteredAsyncTasks方法都優先。The last value of the AsyncTimeout prior to invoking the ExecuteRegisteredAsyncTasks method takes precedence. 如果非同步工作花費超過AsyncTimeout,後續的工作期間,會叫用ExecuteRegisteredAsyncTasks呼叫會立即逾時。If an asynchronous task takes more than the AsyncTimeout, subsequent tasks invoked during that ExecuteRegisteredAsyncTasks call are timed out immediately.

適用於

另請參閱