Multi-thread (7) exception handling in multiple threads and Exception Handling in multiple threads

Source: Internet
Author: User

Multi-thread (7) exception handling in multiple threads and Exception Handling in multiple threads

In the process of multithreading, apart from thread synchronization, exception handling is also a common problem.

By default, the main thread cannot catch exceptions of asynchronous threads.

The following code:

1 namespace ConsoleApplication29 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // Exception Handling of asynchronous threads 8 try 9 {10 Task. factory. startNew () => 11 {12 throw new Exception ("Asynchronous line and Exception occurred! "); 13}); 14} 15 catch (Exception ex) 16 {17 // 18 Console not captured here. writeLine (ex. toString (); 19} 20 21 Console. readKey (); 22} 23} 24}Common View Code Exception Handling Methods

1. Use try/catch inside the asynchronous thread

The following code:

1 namespace ConsoleApplication29 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // Exception Handling of asynchronous threads 8 // 1, use try/catch 9 Task within the asynchronous thread. factory. startNew () => 10 {11 try12 {13 throw new Exception ("Asynchronous line and Exception occurred! "); 14} 15 catch (Exception ex) 16 {17 Console. writeLine (ex. toString (); 18} 19}); 20 21 Console. readKey (); 22} 23} 24}View Code

Running result:

2. Call the Wait method of the Task.

The following code:

Note:

In addition to calling the Wait method of a Task, exceptions can be captured in the main thread. For a Task with a returned value, you no longer need to call the Wait method as long as you receive the returned value.

1 namespace ConsoleApplication29 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // Exception Handling of asynchronous threads 8 // 2, call the Wait method of the Task 9 try10 {11 var task = Task. factory. startNew () => 12 {13 throw new Exception ("Asynchronous line and Exception occurred! "); 14}); 15 task. wait (); 16} 17 catch (Exception ex) 18 {19 Console. writeLine (ex. toString (); 20} 21 22 Console. readKey (); 23} 24} 25}View Code

 

Running result:

3. Read the Exception attribute of the Task in the ContinueWith method of the Task.

The following code:

1 namespace ConsoleApplication29 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // Exception Handling of asynchronous threads 8 // 3, read the Exception attribute 9 of the Task in the ContinueWith method of the Task. factory. startNew () => 10 {11 throw new Exception ("Asynchronous line and Exception occurred! "); 12 }). continueWith (t => 13 {14 if (t. isFaulted) 15 {16 Console. writeLine (t. exception. innerException); 17} 18}, TaskContinuationOptions. onlyOnFaulted); 19 20 Console. readKey (); 21} 22} 23}View Code

 

Running result:

 4. Set taskschedexception. UnobservedTaskException globally.

If the exceptions in the asynchronous thread are not processed and the Task. Wait method is not called to pass the exceptions to the main thread for processing, the most serious consequences may lead to the overall application crash. For details, refer to the IIS application pool crash caused by System. Threading. Tasks. Task.

Therefore, to ensure that the application will not be suspended due to exceptions in asynchronous threads, we recommend that you set TaskScheduler. UnobservedTaskException globally.

If it is a web program, you can set it in the Global Application_Start event, as shown in the following code:

1 protected override void Application_Start (object sender, EventArgs e) 2 {3 System. threading. tasks. taskScheduler. unobservedTaskException + = (s, v) => 4 {5 v. setObserved (); 6}; 7}View Code

 

Related Article

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.