According to http://blogs.msdn.com/b/pfxteam/archive/2009/05/31/9674669.aspx the tasks which have exceptions that are unhanded should tear down the application.
However in Task unhandled exceptions the accepted answer states that in .NET 4.5 the behavior was changed to not tear down the application, I am trying to reproduce the crash behavior in MSVS 2013 and I can't get the application to crash while targetting .NET 4.5 (which is expected) but even when I target .NET 4.0 the application still keeps going after I check through weak reference that the task no longer is alive.
class Program
{
private static WeakReference _ThrowingExceptionOnATaskRun()
{
Task t = Task.Factory.StartNew(() =>
{
Console.WriteLine("Throwing exception in a task!");
throw new NotImplementedException("Not implemented");
});
return new WeakReference(t);
}
static void MyMain()
{
Console.WriteLine("Main start");
WeakReference weakReference = _ThrowingExceptionOnATaskRun();
CheckIfAlliveForceGc(weakReference);
CheckIfAlliveForceGc(weakReference);
CheckIfAlliveForceGc(weakReference);
CheckIfAlliveForceGc(weakReference);
Console.WriteLine("Enter something:");
string userInput = Console.ReadLine();
Console.WriteLine("You entered : {0}", userInput);
Console.WriteLine("Done...");
Console.Read();
}
private static void CheckIfAlliveForceGc(WeakReference weakReference)
{
Console.WriteLine("Is reference alive : {0}", weakReference.IsAlive);
Thread.Sleep(2000);
GC.Collect();
GC.WaitForPendingFinalizers();
}
static void Main(string[] args)
{
MyMain();
}
}
Output
Main start
Is reference alive : True
Throwing exception in a task!
Is reference alive : False
Is reference alive : False
Is reference alive : False
Enter something:
hello
You entered : hello
Done...