异步multithreadingexception处理?

我想在我的异步编程(beginInvoke / endInvoke)中有一个exception处理方法,其中如果任何一个线程(beginInvoke)失败,那么我希望所有其他异步处理线程停止工作。 请建议一些解决方案?,下面我也附上我的示例代码:

public List SendMailAsynch(List requestDto) { List resultDto = new List(); List asyncResults = new List(); foreach (ThreadRequestDto t in requestDto) { //Create a delegate. DoSomeAsynchWorkDelegate del = new DoSomeAsynchWorkDelegate(DoSomeAsynchWork); // Initiate the asynchronous call IAsyncResult a = del.BeginInvoke(t,null, del); //IAsyncResult a = del.BeginInvoke(t, null,null); asyncResults.Add(a); } foreach (IAsyncResult ar in asyncResults) { // wait for each one to complete, then call EndInvoke, passing in the IAsyncResult. // We cast ar.AsyncState to a DoSomeAsynchWorkDelegate, as we passed it in as the second parameter to BeginInvoke. ar.AsyncWaitHandle.WaitOne(); //AsyncState property of IAsyncResult is used to get the delegate that was used to call that method DoSomeAsynchWorkDelegate del = (DoSomeAsynchWorkDelegate)ar.AsyncState; // Call EndInvoke to get the result. Add the result to the list of items. resultDto.Add(del.EndInvoke(ar)); } return resultDto; } 

最好的方法可能是使用共享的ManualResetEvent

例如:

 class MyClass { private ManualResetEvent workFailedEvent = new ManualResetEvent(false); public List SendMailAsynch(List requestDto) { workFailedEvent.Reset(); // --- The rest of your code as written in your post --- } private void DoAsyncWorkFirst() { try { for (int i = 0; i < 10000; i++) { if (workFailedEvent.WaitOne(0, true)) { break; } // -- Do some work here --- } } catch (MyException) { workFailedEvent.Set(); } } private void DoAsyncWorkSecond() { try { for (int j = 0; j < 20000; j++) { if (workFailedEvent.WaitOne(0, true)) { break; } // --- Do some different work here --- } } catch (MyOtherException) { workFailedEvent.Set(); } } } 

这里有趣的部分是对WaitOne的调用(0,true) 。 如果使用超时0,则线程不会阻塞。 由于操作系统同步了ManualResetEvent ,因此这种特殊的方法调用是检查信号的便捷方式,无需担心竞争条件或实现自己的锁定。