title: Async and Await with For Loop
body: <p>I have a Windows Service that runs various jobs based on a schedule. After determining which jobs to run, a list of schedule objects is sent to a method that iterates through list and runs each job. The problem is that some jobs can take up to 10 minutes to run because of external database calls. </p>
  
  <p>My goal is to not have one job block others in queue, basically have more than one run at a time. I thought that using async and await could be to solve this, but I've never used these before. </p>
  
  <p>Current Code:</p>
  
  <pre><code>public static bool Load(List&lt;Schedule&gt; scheduleList)
  {
      foreach (Schedule schedule in scheduleList)
      {
          Load(schedule.ScheduleId);
      }
  
      return true;
  }
  
  public static bool Load(int scheduleId)
  {
      // make database and other external resource calls 
      // some jobs run for up to 10 minutes   
  
      return true;
  }
  </code></pre>
  
  <p>I tried updating to this code:</p>
  
  <pre><code>public async static Task&lt;bool&gt; LoadAsync(List&lt;Schedule&gt; scheduleList)
  {
      foreach (Schedule schedule in scheduleList)
      {
          bool result = await LoadAsync((int)schedule.JobId, schedule.ScheduleId);
      }
  
      return true;
  }
  
  public async static Task&lt;bool&gt; LoadAsync(int scheduleId)
  {
      // make database and other external resource calls 
      // some jobs run for up to 10 minutes   
  
      return true;
  }
  </code></pre>
  
  <p>The issue is that the first LoadAsync waits for the job to finish before giving control back to the loop instead of allowing all the jobs to start. </p>
  
  <p>I have two questions:</p>
  
  <ol>
  <li>High Level - Are aysnc/await the best choice, or should I use a different approach?</li>
  <li>What needs to be updated to allow the loop to kick off all the jobs without blocking, but not allow the function to return until all jobs are completed?</li>
  </ol>