可以在ASP.NET用户控件中使用void async方法吗?
|
我已成功在ASP.NET Web窗体页面上使用async void方法.但是,当我尝试在Web用户控件中使用相同的方法,然后将此Web用户控件放到具有async =“true”设置的页面时,我不断收到此错误:
所以问题是,是否可以在Web用户控件中使用async方法并将其嵌入到启用异步的Web表单页面中? 解决方法如果它在页面上,您可以拥有一个控制调用异步方法.请参阅以下示例:Page.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page.aspx.cs" Inherits="AsyncDEmo.Page" Async="true" %>
<%@ Register Src="~/Control.ascx" TagPrefix="uc1" TagName="Control" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<uc1:Control runat="server" id="Control" />
</form>
</body>
</html>
Page.aspx的代码隐藏是空的 Control.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Control.ascx.cs" Inherits="AsyncDEmo.Control" %>
<span id="TaskMessage" runat="server">
</span>
控制代码隐藏 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();
}
SlowClass.cs 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.";
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – ASP.NET MVC中的小写URL
- asp.net – 在集成模式下替换HttpContext.Curren
- asp.net-mvc – 如果抛出自定义异常,则重定向asp
- asp.net-mvc – HttpPost和HttpGet属性在MVC:为
- asp.net-mvc – ASP.NET MVC – 处理没有返回对象
- asp.net-mvc – RenderPartial从另一个控制器(和
- asp.net-mvc – 带有存储库和ninject的ASP.NET M
- asp.net-mvc – 在我的ASP.NET MVC网站中缓存不能
- asp.net-mvc – ASP.NET MVC与XSL
- asp.net – Mono apache2 =服务暂时不可用(503)
