Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
188 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...