title: Use Task.WaitAll in C# and add the output to a list
body: <p>I wish to use Tasks in C# to simultaneously load multiple data feeds from an online API. I've achieved this using tasks and currently use, the commented out code:</p>
  
  <pre><code>var task = Task.Run(() =&gt; LoadDocument("http://Website.com/" + ID + ".xml?key=" + API.BUSKEY));
  Documents.Add(await task);
  </code></pre>
  
  <p>This code works, however is looped inside of a for loop and and waits for each individual task to complete before continuing to the next; making my use of tasks redundant. </p>
  
  <p>Instead I want to use the Task.WaitAll command and only wait once all tasks have been started. However, I'm having the issue of then adding the output/ results of the tasks to the Documents List. </p>
  
  <p>How can I use the Task.All command and then add all the output to the Documents list? Thank You.</p>
  
  <pre><code>   static void Main(string[] args)
      {
          List&lt;long&gt; Ids = new List&lt;long&gt;()
  
          for (int i = 0; i &lt; 1000; i++)  //Populate 1000 different IDs.
          {
              Ids.Add(039027710001);  //In production these would be all different/ unequie.
          }
  
          var ListOfDocuments = Task.Run(() =&gt; LoadMultiple(Ids)).Result; 
      }
  
      public static async Task&lt;List&lt;XDocument&gt;&gt; LoadMultiple(List&lt;long&gt; IDs)
      {
          List&lt;XDocument&gt; Documents = new List&lt;XDocument&gt;();
          List&lt;Task&gt; Tasks = new List&lt;Task&gt;();
  
          foreach (var ID in IDs)
          {
              Tasks.Add(Task.Run(() =&gt; LoadDocument("http://Website.com/" + ID + ".xml?key=" + API.BUSKEY)));
  
              //This is what I originally had and worked. However waited for each task
              //var task = Task.Run(() =&gt; LoadDocument("http://Website.com/" + ID + ".xml?key=" + API.BUSKEY));
              //Documents.Add(await task);
          }
  
  
          Task.WaitAll(Tasks.ToArray());
          //This is what I want to work and add the idividual results to documents list ^^^
  
          return Documents;
      }
  
  
      public static XDocument LoadDocument(string URL)
      {
          XDocument LiveTimes = XDocument.Load(URL);
          return LiveTimes;
      }
  </code></pre>