gpt4 book ai didi

c# - 异步多线程异常处理?

转载 作者:太空狗 更新时间:2023-10-29 21:56:03 25 4
gpt4 key购买 nike

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

public List<ThreadResultDto> SendMailAsynch(List<ThreadRequestDto>  requestDto)
{
List<ThreadResultDto> resultDto = new List<ThreadResultDto>();
List<IAsyncResult> asyncResults = new List<IAsyncResult>();

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<ThreadResultDto> SendMailAsynch(List<ThreadRequestDto> 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 由操作系统同步,此特定方法调用是检查信号的便捷方式,无需担心竞争条件或实现您自己的锁定。

关于c# - 异步多线程异常处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1354447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com