title: c# Task.WhenAll(tasks) and SemaphoreSlim - how to know when all tasks have fully completed
body: <p>I am having an issue with Task/Thread management in C# and I'm wondering if there's a simple solution to my problem.</p>
  
  <p>In my Windows Phone application, I create a bunch of "upload" tasks. Each task has a progress/completion handler attached, in which I check the status of the task, and if completed successfully, I need to perform a number of thread-safe file write operations.</p>
  
  <p>Pseudo-code:</p>
  
  <p>This snippet is from the method which sets up my tasks and starts them running. I expect this method only to return to caller when all the tasks have fully completed:</p>
  
  <pre><code>var progressCallback = new Progress&lt;UploadOperation&gt;(UploadProgress);
  for (var i = 0; i &lt; uploads.Count; i++)
  {
      uploadTasks[i] = uploads[i].StartAsync().AsTask(ct, progressCallback);
  }
  await Task.WhenAll(uploadTasks);
  // all uploads complete!
  return;
  </code></pre>
  
  <p>My progress/task complete handler checks the status, and if OK, I raise an event which triggers a call to a thread-safe method which needs to perform some file-writes:</p>
  
  <pre><code>private void UploadProgress(UploadOperation upload){
    ....
    if(upload == successful)
    {
        //raise an event which results in a call to a thread-safe method
        //to perform post-upload tasks          
    }
    ....
  }
  </code></pre>
  
  <p>And this is my thread-safe method (triggered by the above event), in which I use a SemaphoreSlim object to ensure that exactly only one thread at a time can access it:</p>
  
  <pre><code>private static readonly SemaphoreSlim _SemaphoreSlim = new SemaphoreSlim(1);
  private async void OnItemUploadOperationCompleted(...)
  {
      await _SemaphoreSlim.WaitAsync();
      try
      {
         //perform some await-able file updates / writes
      }
      catch(..){}
  
      finally
      {
          //release lock
          _SemaphoreSlim.Release();
      }
  </code></pre>
  
  <p>The difficulty I am having is that the main "Task setup" method returns and exits before all of the Upload Tasks have completed their turn in the thread-safe method, i.e. We hit the return statement below while several of the Tasks still haven't taken their turn in the OnItemUploadOperationCompleted method.</p>
  
  <pre><code>await Task.WhenAll(uploadTasks);
  // all uploads complete!
  return;
  </code></pre>
  
  <p>I'm trying to work out if there's a better way to do this. Is there a way of knowing for sure that all Tasks have "fully" completed, that they aren't still hanging and waiting in a queue to enter a thread safe operation? Basically I need to know when all processing is complete, including all the thread-safe post processing of each Task.</p>
  
  <p>It seems that "Task.WhenAll" returns too early, perhaps when the initial Task itself completes but not when it's sub-task / spawned task has completed?</p>
  
  <p>EDIT:</p>
  
  <p>I followed the suggestion made by Servy (first answer below), like as follows:</p>
  
  <pre><code>foreach (var upload in uploads)
  {
      uploadTasks.Add(upload.StartAsync().AsTask(ct, progressCallback).ContinueWith(task =&gt; ProcessUploadResult(task.Result), ct));
  }
  
  await Task.WhenAll(uploadTasks);
  
  // all uploads complete?
  return;
  </code></pre>
  
  <p>And my ProcessUploadResult method is something like as follows:</p>
  
  <pre><code>private void ProcessUploadResult(UploadOperation uploadResult){
  
   ....
   //pseudo code
   if(uploadResult == success){
  
       //RAISE an Event!
       //The listener of this event processes the result - 
       // - performs some file writes / updates
       // - therefore the handler for this eventy MUST be thread safe.
       OnItemUploadOperationCompleted(this, ...});
  
   }
  }
  </code></pre>
  
  <p>So, my difficulty was, even using this approach, was that the event Handler had still not completed processing ALL of the uploads by the time "Task.WhenAll(...)" returns. There are still threads waiting for access to that Event handler.</p>
  
  <p>So, I think I have figured out a solution and I'm wondering if it is a good solution, using ManualResetEvent:</p>
  
  <pre><code> ....
   //pseudo code
   if(uploadResult == success){
  
       //RAISE an Event!
       //The listener of this event processes the result - 
       // - performs some file writes / updates
       // - therefore the handler for this eventy MUST be thread safe.
  
  
       var wait = new ManualResetEvent(false);
  
       // pass the reference to ManualResetEvent in the event Args
       OnItemUploadOperationCompleted(this, new MyEventArgs {waiter = wait});
  
       wait.WaitOne();
  
   }
  }
  </code></pre>
  
  <p>And now in my handler, I use that ManualResetEvent object to signal to the waiting thread that we are done processing the upload response:</p>
  
  <pre><code>private async void OnItemUploadOperationCompleted(object sender, UploadResultEventArgs e)
  {
          await _SemaphoreSlim.WaitAsync();
          try
          {
               //perform async file writes
          }
          catch{
                 ....
          }
          finally
          {
              //release lock
              _SemaphoreSlim.Release();
  
              //signal we are done here
              var waiter = e.Waiter as ManualResetEvent;
              if (waiter != null)
              {
                  waiter.Set();
              }
          }
  }
  </code></pre>
  
  <p>This finally seems to work for me. I'm wondering though is it an ideal solution?</p>