. Net 4.5 (C #): update of tasks with imperceptible exceptions

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.