在后台线程ASP.NET异常处理 [英] ASP.NET Exception Handling in background threads
问题描述
当我这样做ThreadPool.QueueUserWorkItem,我不想未处理的异常杀死我的整个过程。所以我做这样的事情:
When I do ThreadPool.QueueUserWorkItem, I don't want unhandled exceptions to kill my entire process. So I do something like:
ThreadPool.QueueUserWorkItem(delegate() {
try { FunctionIActuallyWantToCall(); }
catch { HandleException(); }
});
这是推荐的模式呢?好像应该有这样做一个简单的方法。这是在一个asp.net-MVC应用程序,如果这是相关的。
Is this the recommended pattern? It seems like there should be a simpler way to do this. It's in an asp.net-mvc app, if that's relevant.
推荐答案
您需要捕捉回调内部的异常(如在你的例子),以避免传播到调用线程。这是推荐的模式。如果它是一个ASP.NET应用程序,你也可以处理它在的Application_Error
方法的Global.asax
You need to catch the exception inside the callback (as in your example) to avoid propagating into the calling thread. This is the recommended pattern. If it is an ASP.NET application you could also handle it in the Application_Error
method in Global.asax
这篇关于在后台线程ASP.NET异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!