views:

33

answers:

1

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 can't use BackgroundWorker, because I need to 'join' the work, and render the page.

Suggestions? Anyone?

+2  A: 

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();
    }
}
ChaosPandion
Good call Chaos. I was hoping for something more generic (I'd created a LINQ-based DSL for this), but I suppose I can work in an exceptions queue w/out too much trouble. Thanks.
Jeff D