UnobservedTaskException does not kill the process

I am trying to understand the UnobservedTaskException problem in .NET 4.0, so I wrote the folloging code

TaskScheduler.UnobservedTaskException += (sender, eventArgs) => Console.WriteLine("unobserved"); Task.Factory.StartNew(() => { throw new Exception(); }, TaskCreationOptions.LongRunning); using (var autoResetEvent = new AutoResetEvent(false)) { autoResetEvent.WaitOne(TimeSpan.FromSeconds(10)); } Console.WriteLine("Collecting"); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("Still working "); Console.ReadKey(); Console.WriteLine("Still working "); Console.WriteLine("Still working "); Console.ReadKey(); 

UnobservedTaskException is thrown, and my application just continues to work. However, according to MSDN, the process must be killed. Can someone tell me why?

+4
source share
1 answer

If you run this code on a computer that has .Net 4.0 installed, it will actually work.

Since all versions of .Net from version 4.0 have been updated in place, even if you are targeting the application to .Net 4.0, it will work in a later version on a machine with one.

To get the same behavior of .NET 4.0 when running in a later version, you can add this to your app.config file (as described in the TaskScheduler.UnobservedTaskException Event):

 <runtime> <ThrowUnobservedTaskExceptions enabled="true"/> </runtime> 
+4
source

All Articles