How to prevent an exception in a background thread from terminating an application?

后端 未结 5 1113
我寻月下人不归
我寻月下人不归 2020-11-29 22:33

I can hookup to AppDomain.CurrentDomain.UnhandledException to log exceptions from background threads, but how do I prevent them terminating the runtime?

5条回答
  •  一生所求
    2020-11-29 22:50

    From Joe Albahari's excellent threading article:

    The .NET framework provides a lower-level event for global exception handling: AppDomain.UnhandledException. This event fires when there's an unhandled exception in any thread, and in any type of application (with or without a user interface). However, while it offers a good last-resort mechanism for logging untrapped exceptions, it provides no means of preventing the application from shutting down – and no means to suppress the .NET unhandled exception dialog.

    In production applications, explicit exception handling is required on all thread entry methods. One can cut the work by using a wrapper or helper class to perform the job, such as BackgroundWorker (discussed in Part 3).

提交回复
热议问题