问题描述
我想使用异步和asp.net等待。为简单起见,我的目标是asynchronuosly调用方法,一旦返回,在标签更新UI的数据。
下面是Default.aspx的
I am trying to use async and await in asp.net. 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.
推荐答案
当我描述我的博客上,的。考虑这个问题的方法之一是,等待
收益率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.
有关详细信息,请参阅我的。
For more information, see my MSDN article on async
ASP.NET.
这篇关于异步并等待不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!