Can't start ASP.NET Webforms async tasks

I am trying to create an asynchronous task in ASP.NET Webforms. After researching various sources from the internet, I created this:

Default.aspx:

namespace AsyncTestCs
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterAsyncTask(new PageAsyncTask(LoadSomeData));
        }

        public async Task LoadSomeData()
        {
            var downloadedString = await new WebClient().DownloadStringTaskAsync("http://localhost:59850/WebForm1.aspx");

            Label1.Text = downloadedString;
        }
    }
}

      

WebForm1.aspx:

namespace AsyncTestCs
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Thread.Sleep(5000);
            Response.Write("something");
        }
    }
}

      

but it doesn't work asynchronously. This page will be displayed after downloadString is loaded.

Where is my mistake?

+3


source to share


1 answer


Server code is not tied to controls on the client. ASP.NET generates HTML as part of its processing and sends it to the client. After that, all ASP.NET objects associated with the request die. HTTP is request-response based. No permanent connection.

What are you doing here, wait 5 seconds, then customize the label text and then send HTML with that label text to the client.



Async has nothing to do with delaying updates to the client.

+2


source







All Articles