Await multiple async Task while setting max running task at a time

后端 未结 4 977
失恋的感觉
失恋的感觉 2020-12-19 12:19

So I just started to try and understand async, Task, lambda and so on, and I am unable to get it to work like I want. With the code below I want for it to lock btnDoWebReque

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 12:42

    First, don't use Task.Factory.StartNew by default. In fact, this should be avoided in async code. If you need to execute code on a background thread, then use Task.Run.

    In your case, there's no need to use Task.Run (or Task.Factory.StartNew).

    Start at the lowest level and work your way up. You already have an asynchronous web-requesting method, which I'll rename to WebRequestAsync to follow the Task-based Asynchronous Programming naming guidelines.

    Next, throttle it by using the asynchronous APIs on SemaphoreSlim:

    await maxThread.WaitAsync();
    try
    {
      await Global.WebRequestWorkAsync(i);
    }
    finally
    {
      maxThread.Release();
    }
    

    Do that for each request info (note that no background thread is required):

    private async Task DoWebRequestsAsync()
    {
      List requestInfoList = new List();
      for (int i = 0; dataRequestInfo.RowCount - 1 > i; i++)
      {
        requestInfoList.Add((requestInfo)dataRequestInfo.Rows[i].Tag);
      }
      await Task.WhenAll(requestInfoList.Select(async i => 
      {
        await maxThread.WaitAsync();
        try
        {
          await Global.WebRequestWorkAsync(i);
        }
        finally
        {
          maxThread.Release();
        }
      }));
    }
    

    Finally, call this from your UI (again, no background thread is required):

    private async void btnDoWebRequest_Click(object sender, EventArgs e)
    {
      btnDoWebRequest.Enabled = false;
      await DoWebRequestsAsync();
      btnDoWebRequest.Enabled = true;
    }
    

    In summary, only use Task.Run when you need to; do not use Task.Factory.StartNew, and do not use Wait (use await instead). I have an async intro on my blog with more information.

提交回复
热议问题