In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to deal with .NET multithreaded exceptions, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.
Multithreaded applications, in actual project or product development, should, in principle, be avoided (this is my opinion, because I am not a multitasking person). However, under the requirements of emphasizing user experience or under the limitations of the development platform (such as Silverlight Socket communication), we have to use multithreading.
Multithreaded environment
In our product SE, there are two main types of multithreading, one is initiating multithreading actively through ThreadPool or new Thread, and the other is Socket communication callback.
Multithreaded exception capture
For general exception handling, we simply include the possible error statements in the try/catch statement. I have also simply applied this method to multi-threaded exception catch, and the result is not the case, the code is as follows:
Public static void Main () {try {new Thread (Go). Start ();} catch (Exception ex) {/ / will never be executed here! Console.WriteLine ("Exception!");}} private static void Go () {throw null;}
The correct thing to do is to catch exceptions in the new thread entry method Go:
Public static void Main () {new Thread (Go). Start ();} private static void Go () {try {... Throw null; / / the exception will be caught.} catch (Exception ex) {/ / exception logging, or notify other threads that an exception has occurred.}}
The above correct approach comes from the section Exception Handling in Threading in C #, which covers all aspects of .NET multithreading and is the most comprehensive article I have seen.
The correct way to catch multithreaded exceptions is found, and then we naturally wonder: does every thread entry method have to do this?
Let's take a look at the description of Exception Handling in Threading in C #: starting with .NET 2.0, any unhandled exception on any thread will cause the entire application to shut down. Therefore, the try/catch statement must be used in each thread entry method, at least in the production application, lest the application close the entire application because of code that we did not expect.
If you just write down the exception information and don't care about the application exception closing, there are two ways to do this:
1. For Windows Form programs, there is a global exception handling event: Application.ThreadException
2. For all .NET programs, there is a lower-level global exception handling event: AppDomain.UnhandledException
Higher requirements
We can simply log errors through global exception handling events; if we ensure that the application is not interrupted, we can also catch exceptions and log exceptions in each thread entry method. Is there a way to catch exceptions without interrupting the application and as simple as global exception handling events?
For new threads that are actively created, you can at least do this:
Public static class ThreadExecutor {public static bool Execute (System.Threading.WaitCallback callback, object state) {try {return System.Threading.ThreadPool.QueueUserWorkItem ((data) = > {try {callback (data)) } catch (exception ex) {/ / log the exception}}, state);} catch (Exception e) {/ / log the exception} return false }} Thank you for reading this article carefully. I hope the article "how to handle .NET multithreaded exceptions" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.