title: Why Garbage Collector doesn't collect Tasks objects
body: <p>especially when no live Thread reference it.</p>
  
  <p>I thought GC goes thought all .net threads to find references... Does it check references in other places too? </p>
  
  <p>EDIT: FOr instance let's imagine we are in a console app, the main calls a method that creates a local task1, then applies a task1.ContinueWith(task2) and returns to main, main do console.readline().</p>
  
  <p>At this point it could be that task1 has finished, task2 still has not started a GC could start and no thread has a reference to task2. Why task2 doesn't get GC'ed?</p>
  
  <p>EDIT2: Probably I'm not using the right words when saying "task"</p>
  
  <pre><code>using System;
  using System.Runtime.InteropServices;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace ConsoleApplication
  {
      class Program
      {
          static void Launch()
          {
              var task1 = Task.Run(() =&gt; Thread.Sleep(60000))
              task1.ContinueWith(() =&gt;  WriteToFile("Hi"));
          }
  
          static void Main(string[] args)
          {
              Launch();
              //At this point if a GC occurs which thread or static file has a reference to "()=&gt;WriteTofile("Hi")" ?
              Console.ReadLine();
          }
  </code></pre>
  
  <p>There is the main thread waiting for the console, one thread (maybe from the threadpool) running the Sleep. Just after the Sleep is done, and before the WriteToFile thread start, a GC could happen, isn't it?</p>