PageAsyncTask クラス
アセンブリ: System.Web (system.web.dll 内)


ASP.NET Version 2.0 を使用すると、1 つのページに複数のタスクを登録し、ページを表示する前にこれらのタスクを非同期に実行できます。処理に時間のかかるタスクがある場合、このタスクの実行中に他のプロセスが停滞しないように、このタスクを非同期で実行するように指定できます。非同期タスクは並列処理することも順次処理することもできます。
PageAsyncTask オブジェクトは、RegisterAsyncTask メソッドを使用してページに登録する必要があります。非同期タスクを実行するために、ページ自体を非同期で処理する必要はありません。ページ ディレクティブで Async 属性を (次のコード例に示すように) true に設定しても false に設定しても、非同期タスクは非同期で処理されます。
Async 属性を false に設定した場合、すべての非同期タスクが完了するまで、ページを実行するスレッドがブロックされます。
PreRenderComplete イベントの前に登録された非同期タスクがまだ実行されていない場合、ページによって自動的に実行されます。PreRenderComplete イベントの後に登録された非同期タスクは、ExecuteRegisteredAsyncTasks メソッドを使用して明示的に実行する必要があります。ExecuteRegisteredAsyncTasks メソッドを使用して、PreRenderComplete イベントの前にタスクを開始することもできます。ExecuteRegisteredAsyncTasks メソッドにより、ページに登録されている、まだ実行されていないすべての非同期タスクが実行されます。
既定では、非同期タスクは 45 秒以内に完了しなかった場合、タイムアウトになります。Web.config ファイルまたはページ ディレクティブで別のタイムアウト値を指定できます。Web.config ファイルの <pages> セクションでは、次に示すように、asyncTimeout 属性が定義されます。
</pages>
</system.web>

3 つの非同期タスクをページに登録し、並列で処理する方法を次のコード例に示します。各タスクは、単にスレッドを 5 秒間スリープ状態にするメソッドを呼び出します。
<%@ 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() 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) ' Execute the register asynchronous task. Page.ExecuteRegisteredAsyncTasks() TaskMessage.InnerHtml = slowTask1.GetAsyncTaskProgress() + "<br>" + slowTask2.GetAsyncTaskProgress() + "<br>" + slowTask3.GetAsyncTaskProgress() End Sub </script> <html > <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>
<%@ Page Language="C#" Async="true" AsyncTimeout="35"%> <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(); 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); // Execute the register asynchronous task. Page.ExecuteRegisteredAsyncTasks(); TaskMessage.InnerHtml = slowTask1.GetAsyncTaskProgress()+ "<br>" + slowTask2.GetAsyncTaskProgress() + "<br>" + slowTask3.GetAsyncTaskProgress(); } </script> <html > <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>
Imports Microsoft.VisualBasic 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
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."; } } }


System.Web.UI.PageAsyncTask


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- PageAsyncTask クラスのページへのリンク