title: Using IProgress when reporting progress for async await code vs progress bar control
body: <pre><code>private static async Task FuncAsync(DataTable dt, DataRow dr)
  {
      try
      {
          await Task.Delay(3000); //assume this is an async http post request that takes 3 seconds to respond
          Thread.Sleep(1000) //assume this is some synchronous code that takes 2 second
      }
      catch (Exception e)
      {
          Thread.Sleep(1000); //assume this is synchronous code that takes 1 second
      }
  }
  </code></pre>
  <pre><code>private async void Button1_Click(object sender, EventArgs e)
  {
      List&lt;Task&gt; lstTasks = new List&lt;Task&gt;();
  
      DataTable dt = (DataTable)gridview1.DataSource;
  
      foreach (DataRow dr in dt.Rows)
      {
          lstTasks.Add(FuncAsync(dr[&quot;colname&quot;].ToString());                
      }            
  
      while (lstTasks.Any())
      {   
          Task finishedTask = await Task.WhenAny(lstTasks);
          lstTasks.Remove(finishedTask);
          await finishedTask;
          progressbar1.ReportProgress();
      }                        
  }
  </code></pre>
  <p>Assuming the datatable has got 10000 rows.</p>
  <p>In the code, on button click, at the 1st iteration of the for loop, an async api request is made. While it takes 3 seconds, the control immediately goes to the caller. So the for loop can make the next iteration, and so on.</p>
  <p>When the api response arrives, the code below the await runs as a callback. Thus blocking the UI thread and any incomplete for loop iterations will be delayed until the callback completes irrespective of whether I use await <code>WhenAny</code> or <code>WhenAll</code>.</p>
  <p>All code runs on the UI thread due to the presence of synchronization context. I can do <code>ConfigureAwait</code> <code>false</code> on <code>Task.Delay</code> so the callbacks run on separate threads in order to unblock the ui thread.</p>
  <p>Say 1000 iterations are made when the 1st await returns and when the 1st iterations await call back runs the following iterations will have completed completed awaits so their callbacks will run. Effectively callbacks will run one after the other if configure await is true. If false then they will run in parallel on separate threads.</p>
  <p>So I think that the progress bar that I am updating in the while loop is incorrect because - by the time the code reaches the while block, most of the initial for loop iterations will have been already completed. I hope that I have understood correctly so far.</p>
  <p>I have the following options to report progress from inside the task:</p>
  <ol>
  <li><p>using <code>IProgress</code> (I think this is more suitable to report progress from another thread [for example when using <code>Task.Run</code>], or in usual async await if the configure await is false, resulting in code below the await to run in separate thread otherwise it will not show the progress bar moving as the ui thread will be blocked running the callbacks. In my current example code always runs on the same UI thread). So I was thinking the below point may be more appropriate solution.</p>
  </li>
  <li><p>making the Task non-static so that I can access the progress bar from within the Task and do <code>porgressbar1.PerformStep()</code>.</p>
  </li>
  </ol>
  <p>Another thing I have noticed is that <code>await WhenAll</code> doesn't guarantee that <code>IProgress</code> is fully executed.</p>