especially when no live Thread reference it.
I thought GC goes thought all .net threads to find references... Does it check references in other places too?
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().
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?
EDIT2: Probably I'm not using the right words when saying "task"
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Launch()
{
var task1 = Task.Run(() => Thread.Sleep(60000))
task1.ContinueWith(() => WriteToFile("Hi"));
}
static void Main(string[] args)
{
Launch();
//At this point if a GC occurs which thread or static file has a reference to "()=>WriteTofile("Hi")" ?
Console.ReadLine();
}
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?