I have written an articleArticle:
. Net (C #) TPL: the task is not aware of exceptions and taskscheduler. unobservedtaskexception events.
In. Net 4.0, an imperceptible task exception will be thrown in the terminator execution thread during garbage collection. Like other unprocessed exceptions, this exception will cause the entire process to crash. This changed after. NET Framework 4.5 BETA, and this imperceptible exception won't cause a process crash.
So the following exceptions will occur in. NET Framework 4.0.CodeNo exception occurs in. NET Framework 4.5 BETA.
// Throw an exception in the task
Task.Factory.Startnew (()=>
{
Throw New Exception();
});
// Make sure the task is completed
Thread.Sleep (100);
// Force the spam to receive
GC.Collect ();
// Wait for the terminator to process
GC.Waitforpendingfinalizers ();
If you want to maintain. NET Framework 4.5 in. NET Framework 4.0 Beta, you canProgramIn the configuration file, set throwunobservedtaskexceptions to true (the default value is false.
<Configuration>
<Runtime>
<Throwunobservedtaskexceptions Enabled="True"/>
</Runtime>
</Configuration>
When running the code above, the exception will be thrown again and cause a process crash.