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

后端 未结 5 1108
我寻月下人不归
我寻月下人不归 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:44

        AppDomain.CurrentDomain.UnhandledException += (sender, e2) =>
        {
            Thread.CurrentThread.Join();
        };
    

    But be careful, this code will freeze all the stack memory of the Thread and thread's managed object itself. However, if your application is in determined state (may be you threw LimitedDemoFunctionalityException or OperationStopWithUserMessageException) and you are not developing 24/7 application this trick will work.

    Finally, I think MS should allow developers to override the logic of unhandled exceptions from the top of the stack.

提交回复
热议问题