views:

24

answers:

1

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(); }
});

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.

+1  A: 

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

Darin Dimitrov