Remarque : cette méthode est nouvelle dans le .NET Framework version 2.0.
Démarre l'exécution d'une tâche asynchrone.
Espace de noms : System.Web.UI
Assembly : System.Web (dans system.web.dll)
Visual Basic (Déclaration)
Public Sub ExecuteRegisteredAsyncTasks
Visual Basic (Utilisation)
Dim instance As Page
instance.ExecuteRegisteredAsyncTasks
public void ExecuteRegisteredAsyncTasks ()
public:
void ExecuteRegisteredAsyncTasks ()
public void ExecuteRegisteredAsyncTasks ()
public function ExecuteRegisteredAsyncTasks ()
Définissez une tâche asynchrone à l'aide de la classe PageAsyncTask. Après la définition de la tâche et son enregistrement avec la page à l'aide de la méthode RegisterAsyncTask, la méthode ExecuteRegisteredAsyncTasks peut être appelée pour commencer la tâche asynchrone.
La méthode ExecuteRegisteredAsyncTasks est appelée automatiquement lors du traitement de la page au moment de l'appel des éventuelles tâches asynchrones enregistrées pour une page non asynchrone. Cet appel automatique à ExecuteRegisteredAsyncTasks se produit juste avant l'événement PreRenderComplete. Appelez la méthode ExecuteRegisteredAsyncTasks pour des tâches que vous souhaitez appeler en dehors de l'appel automatique de cette méthode. Notez que les tâches asynchrones ne seront exécutées qu'une seule fois bien que ExecuteRegisteredAsyncTasks puisse être appelé plusieurs fois.
La propriété AsyncTimeout est redéfinie sur chaque appel à la méthode ExecuteRegisteredAsyncTasks. La dernière valeur de AsyncTimeout avant d'appeler la méthode ExecuteRegisteredAsyncTasks a la priorité. Si une tâche asynchrone prend plus que le AsyncTimeout, les tâches suivantes appelées pendant cet appel ExecuteRegisteredAsyncTasks arrivent immédiatement à expiration.
L'exemple de code suivant illustre l'utilisation de la propriété AsyncTimeout avec les méthodes ExecuteRegisteredAsyncTasks et RegisterAsyncTask. Notez l'utilisation des gestionnaires de début, de fin et de délai d'attente. Dans l'exemple, un délai artificiel est introduit pour illustrer le cas d'une tâche asynchrone qui dépasse le temps alloué à la tâche et spécifié dans la propriété AsyncTimeout. Dans un scénario réel, une tâche asynchrone peut être utilisée, par exemple, pour exécuter des appels de base de données ou des générations d'image, et le gestionnaire de délai d'attente assure une dégradation progressive si la tâche n'est pas effectuée dans le temps imparti.
<%@ Page Language="VB" AsyncTimeout="2"%>
<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 >
<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="C#" AsyncTimeout="2"%>
<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 >
<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>
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
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.";
}
}
}
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
.NET Framework
Prise en charge dans : 2.0