title: How to wait until all the tasks finish before returning from method in C#
body: <p>In my Web API Controller <strong>MyController</strong> there is a call to my service class </p>
  
  <pre><code>   [HttpPost]
     Route("groupmembershipvalidate")]
     public IHttpActionResult PostGroupMembershipValidate(ListGroupMembershipUploadInput ListGroupMembershipUploadInput) 
     {
  
      //perform some tasks
  
      var searchResults =  ts.validateDetails(gmvi);
  
      return Ok(searchResults);
  
     }
  </code></pre>
  
  <p>Now this <strong>validateDetails</strong> service class needs to run some tasks parallely and needs to wait for all the results to come back and then only  return back .</p>
  
  <pre><code>        var tasks = new Task[]
          {
              Task.Factory.StartNew(() =&gt; gd.validateChapterCodeDetails(_input1)),
              Task.Factory.StartNew(() =&gt; gd.validateGroupCodeDetails(_input1)),
              Task.Factory.StartNew(() =&gt; gd.validateMasterIdDetails(_input1))
          };
  
         var things = Task.WhenAll(tasks);
  
         //Some tasks
  
         return result;
  </code></pre>
  
  <p>The result returned is not computed until all the tasks return their results. But when Task.WhenAll(tasks) is hit , it immediately moves to the next few lines and returned to  <strong>PostGroupMembershipValidate</strong> controller method . </p>
  
  <p>How to wait before returning values and return only after getting all the values ?</p>