C# async tasks waiting indefinitely

后端 未结 2 857
醉梦人生
醉梦人生 2021-02-20 01:48

I am trying to use the functionality provided by \"async\" & \"await\" to asynchronously download webpage content and I have into issues where the Tasks are waiting forever

2条回答
  •  时光说笑
    2021-02-20 02:24

    +1 Stephen Cleary. Just came to know you need to have async before void type with Page_Load as given below:

    protected async void Page_Load(object sender, EventArgs e)
    {
       var tasks = websites.Select(GenerateSomeContent);
       await Task.WhenAll(tasks);
    }
    

    And then in your code-behind file (in case asp.net web form app) should also have Async="true" attribute.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Async="true" Inherits="EmptyWebForm._default" %>
    

    Hope this helps visitors.

提交回复
热议问题