Passing exceptions across threads in an MVC application
I need to call a few webservices while constructing an MVC page, so I'm threading the work via the threadpool, and using ManualResetEvents to to determine when I have my results.
If one of the threads throws an exception though, it'll crash the app. I have exception handling set up via a Filter, but I need to get the exception back to the primary rendering thread first.
I开发者_JAVA技巧 can't use BackgroundWorker, because I need to 'join' the work, and render the page.
Suggestions? Anyone?
You can keep a queue with the exceptions and when the ManualResetEvent
is finally set check the queue before you continue.
private readonly Queue<Exception> _exceptions = new Queue<Exception>();
private void DoWork(object o)
{
try
{
// ...
}
catch (Exception ex)
{
_exceptions.Enqueue(ex);
}
finally
{
done.Set();
}
}
精彩评论