Based on my research, I have learned the following:
TaskScheduler.UnobservedTaskException
must wait for the task to be garbage collected before th
Nathan,
You're points are all true. Try the following:
namespace ConsoleApplication1
{
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
Task.Factory.StartNew(() =>
{
Console.WriteLine("Task started.");
throw new Exception("Test Exception");
});
Thread.Sleep(1000);
Console.WriteLine("First Collect");
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Waiting");
Console.ReadKey();
}
static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
Console.WriteLine("UNOBSERVED EXCEPTION");
}
}
}
I have noticed that the debugger often "traps" the UnobservedTaskException event, causing it to not fire appropriately. Run this outside of the debugger, and it will print "UNOBSERVED EXCEPTION" every time prior to shutting down.