async and await is not working

冷暖自知 提交于 2019-12-13 08:17:43

问题


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.


回答1:


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.




回答2:


Async and Await are used to process multiple web requests in parallel. These methods come into once the request reaches the server and starts competing with other incoming web requests.

These should not be mistaken for asynchronous web request. That can be achieved using ajax.



来源:https://stackoverflow.com/questions/29278512/async-and-await-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!