异步并等待不工作

编程入门 行业动态 更新时间:2024-10-19 19:39:38
本文介绍了异步并等待不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用异步和asp等待。为简单起见,我的目标是asynchronuosly调用方法,一旦返回,在标签更新UI的数据。下面是Default.aspx的

I am trying to use async and await in asp. for simplicity, my objective is call a method asynchronuosly and once return, update the data in label on UI. Here is default.aspx

<form id="form1" runat="server"> <div> <asp:Button runat="server" Text="click me" OnClick="asyncbtn_Click" id="asyncbtn" /><br /> <asp:TextBox runat="server" /><br /> <asp:Label runat="server" Text="[Result label]" ID="resultLabel"/> <asp:Label runat="server" Text="[Result label]" ID="Label1"/> </div> </form>

隐藏文件code ...

code behind file...

protected void asyncbtn_Click(object sender, EventArgs e) { RegisterAsyncTask(new PageAsyncTask(DoSomethingAsync)); } public async Task<int> DoSomethingAsync() { await Task.Delay(10000); resultLabel.Text = 20.ToString(); await Task.Delay(5000); Label1.Text = 30.ToString(); return 0; }

所以,当我点击按钮,我的浏览器等到整个DoSomethingAsync方法完成。所以我相信,这将成为同步调用不是异步的。

So when I click on button, my browser wait until entire DoSomethingAsync method is completed. so i believe this will become sync call not async one.

谁能告诉我什么是错在这里。

Can anyone tell me what's wrong here.

推荐答案

当我描述我的博客上,的 异步不改变HTTP协议。考虑这个问题的方法之一是,等待收益率ASP.NET运行时,的不的浏览器。

As I describe on my blog, async does not change the HTTP protocol. One way of thinking about it is that await yields to the ASP.NET runtime, not to the browser.

有关详细信息,请参阅我的 MSDN文章异步 ASP.NET 。

For more information, see my MSDN article on async ASP.NET.

更多推荐

异步并等待不工作

本文发布于:2023-11-08 20:27:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570373.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!