Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

asp.net - async and await is not working

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...