title: AttachedToParent Task confusion
body: <p>I have a problem understanding how <code>AttachedToParent</code> parameter works. </p>
  
  <p>Here is the sample code:</p>
  
  <pre><code>public static void Main(string[] args)
      {
          Task&lt;int[]&gt; parentTask = Task.Run(()=&gt; 
          {
              int[] results = new int[3];
  
              Task t1 = new Task(() =&gt; { Thread.Sleep(3000); results[0] = 0; }, TaskCreationOptions.AttachedToParent);
              Task t2 = new Task(() =&gt; { Thread.Sleep(3000); results[1] = 1; }, TaskCreationOptions.AttachedToParent);
              Task t3 = new Task(() =&gt; { Thread.Sleep(3000); results[2] = 2; }, TaskCreationOptions.AttachedToParent);
  
              t1.Start();
              t2.Start();
              t3.Start();
  
              return results;
          });
  
          Task finalTask = parentTask.ContinueWith(parent =&gt;
          {
              foreach (int result in parent.Result)
              {
                  Console.WriteLine(result);
              }
          });
  
          finalTask.Wait();
          Console.ReadLine();
      }
  </code></pre>
  
  <p>As I understand, when a Task has child Tasks, the parent Task finishes when all the child tasks are ready. The problem with this example is that the output looks like this:</p>
  
  <pre><code>0
  0
  0
  </code></pre>
  
  <p>This means that the parent Task was not waiting for its child tasks to finish. The only way to get a valid result <code>0 1 2</code> is to use Wait on all of the child Taks, by adding some piece of code like this before the <code>return results;</code> statement:</p>
  
  <pre><code>Task[] taskList = { t1, t2, t3 };
  Task.WaitAll(taskList);
  </code></pre>
  
  <p>My question is this. Why do we use <code>TaskCreationOptions.AttachedToParent</code> when we also have to manually call Wait method for every child Task?  </p>
  
  <p><strong>Edit:</strong></p>
  
  <p>While I was writing this question, I've changed code a little bit and now AttachedToParent works well. The only difference is that I've used <code>parentTask.Start();</code> instead of the <code>Task.Run();</code>.</p>
  
  <pre><code>public static void Main(string[] args)
      {
          Task&lt;int[]&gt; parentTask = new Task&lt;int[]&gt;(()=&gt; 
          {
              int[] results = new int[3];
  
              Task t1 = new Task(() =&gt; { Thread.Sleep(3000); results[0] = 0; }, TaskCreationOptions.AttachedToParent);
              Task t2 = new Task(() =&gt; { Thread.Sleep(3000); results[1] = 1; }, TaskCreationOptions.AttachedToParent);
              Task t3 = new Task(() =&gt; { Thread.Sleep(3000); results[2] = 2; }, TaskCreationOptions.AttachedToParent);
  
              t1.Start();
              t2.Start();
              t3.Start();
  
              //Task[] taskList = { t1, t2, t3 };
              //Task.WaitAll(taskList);
  
              return results;
          });
  
          parentTask.Start();
  
          Task finalTask = parentTask.ContinueWith(parent =&gt;
          {
              foreach (int result in parent.Result)
              {
                  Console.WriteLine(result);
              }
          });
  
          finalTask.Wait();
          Console.ReadLine();
      }
  </code></pre>
  
  <p>I still don't understand why there is a problem with the first example.   </p>