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 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;
}
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…