title: Task.WaitAll() on continuation task only delays execution of original task?
body: <p><strong>Background:</strong></p>
  
  <p>I've got a console app which creates <code>Tasks</code> to process data from a DB (let's call them Level1 tasks). Each one of the Tasks creates its own Tasks again to process each part of the data that was assign to it (Level2 tasks).</p>
  
  <p>Each of the Level2 tasks has a continuation task associated with it and the code used to do a <code>WaitAll</code> on the continuation tasks before moving on.</p>
  
  <p>I'm on <code>.NET 4.0</code> (no <code>async</code>/<code>await</code>)</p>
  
  <p><strong>Issue:</strong></p>
  
  <p>This created a problem though - it turned out that if it is done this way, none of the Level2 tasks was started before all available Level1 tasks were scheduled. Which is not optimal in any way.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>This seems to be fixed with changing the code to wait for both the original Level2 task AND its continuation task. However, I'm not entirely sure why this is the case.</p>
  
  <p>Do you have any ideas?</p>
  
  <p>The only thing that I could come up with is that - since the continuation task has not started, there is no point in waiting for it to complete. But even if that was the case I would expect at least SOME of the Level2 tasks to have started. Which they never did.</p>
  
  <p><strong>Example:</strong></p>
  
  <p>I created a sample console app that demonstrated exactly that behavior:</p>
  
  <ol>
  <li><p>Run it as is and you will see that it's scheduling all the tasks first, and only then you start getting the actual lines written from within the Level2 tasks.</p></li>
  <li><p>But comment out the marked block of code and uncomment the replacement and all works as expected.</p></li>
  </ol>
  
  <p>Can you tell me why?</p>
  
  <pre><code>public class Program
  {
      static void Main(string[] args)
      {
          for (var i = 0; i &lt; 100; i++)
          {
              Task.Factory.StartNew(() =&gt; SomeMethod());
              //Thread.Sleep(1000);
          }
  
          Console.ReadLine();
      }
  
      private static void SomeMethod()
      {
          var numbers = new List&lt;int&gt;();
  
          for (var i = 0; i &lt; 10; i++)
          {
              numbers.Add(i);
          }
  
          var tasks = new List&lt;Task&gt;();
  
          foreach (var number in numbers)
          {
              Console.WriteLine("Before start task");
  
              var numberSafe = number;
  
              /* Code to be replaced START */
  
              var nextTask = Task.Factory.StartNew(() =&gt;
              {
                  Console.WriteLine("Got number: {0}", numberSafe);
              })
                  .ContinueWith(task =&gt;
                  {
                      Console.WriteLine("Continuation {0}", task.Id);
                  });
  
              tasks.Add(nextTask);
  
              /* Code to be replaced END */
  
              /* Replacement START */
  
              //var originalTask = Task.Factory.StartNew(() =&gt;
              //{
              //    Console.WriteLine("Got number: {0}", numberSafe);
              //});
  
              //var contTask = originalTask
              //    .ContinueWith(task =&gt;
              //    {
              //        Console.WriteLine("Continuation {0}", task.Id);
              //    });
  
              //tasks.Add(originalTask);
              //tasks.Add(contTask);
  
              /* Replacement END */
          }
  
          Task.WaitAll(tasks.ToArray());
      }
  }
  </code></pre>