Page.AsyncTimeout 属性
定义
获取或设置一个值,它指示处理异步任务时使用的超时间隔。Gets or sets a value indicating the time-out interval used when processing asynchronous tasks.
public:
property TimeSpan AsyncTimeout { TimeSpan get(); void set(TimeSpan value); };
[System.ComponentModel.Browsable(false)]
public TimeSpan AsyncTimeout { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.AsyncTimeout : TimeSpan with get, set
Public Property AsyncTimeout As TimeSpan
属性值
一个 TimeSpan,包含完成异步任务所允许的时间间隔。A TimeSpan that contains the allowed time interval for completion of the asynchronous task. 默认时间间隔为 45 秒。The default time interval is 45 seconds.
- 属性
例外
该属性设置为负值。The property was set to a negative value.
示例
下面的代码示例演示如何将 AsyncTimeout 属性与 ExecuteRegisteredAsyncTasks 和方法一起使用 RegisterAsyncTask 。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. 请注意,在 AsyncTimeout page 指令中设置了属性。Note that the AsyncTimeout property is set in the page directive.
<%@ 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
注解
页面的异步超时表示页面将等待执行异步任务的时间量。The asynchronous time-out of the page represents the amount of time that the page will wait to perform asynchronous tasks. 在大多数情况下,不要在代码中设置此属性。In most circumstances, do not set this property in code. 使用 Web 配置文件或@ page指令中的pages 元素设置页面异步超时间隔。Set the page asynchronous time-out interval using the pages element of the Web configuration file or in the @ Page directive. 页面指令将覆盖页面配置节中设置的值。Values set in the page configuration section are overwritten by the page directive.
使用类定义异步任务 PageAsyncTask 并注册开始时间、结束时间和超时处理程序。Define your asynchronous task using the PageAsyncTask class and register a beginning, an ending, and a time-out handler. 如果异步任务未在指定的时间间隔内完成,则将调用超时处理程序。If the asynchronous task does not complete in the time interval specified, the time-out handler will be invoked.