c# – WaitAll vs WaitAny
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WaitAll vs WaitAny,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1115字,纯文字阅读大概需要2分钟。
内容图文
![c# – WaitAll vs WaitAny](/upload/InfoBanner/zyjiaocheng/806/3bd9fe5345454efea8f743394bf5c793.jpg)
我对WaitAll和WaitAny有点困惑.我试图获得异常,但是当我做WaitAll它返回异常但是当使用WaitAny返回nothing.And必要的是,如果任何任务完成工作完成.是他们的任何替换WaitAny(). WaitAll和WhenAll是不同的因为我不想让所有任务完成.喜欢
try
{
int i = 0;
Task t1 = Task.Factory.StartNew(() =>
{
i = 2 * 4;
});
Task<int> t2 = Task.Factory.StartNew(() =>
{
int a = 0;
int b = 100 / a;
return 0;
});
Task[] tasks = new Task[] { t1, t2 };
Task.WaitAny(tasks);
//Task.WaitAll(tasks);// Working
}
catch (AggregateException ae)
{
var message = ae.InnerException.Message;
Console.WriteLine(message);
}
Console.ReadLine();
解决方法:
这称为Unobserved Exception,这基本上意味着Task Parallel Library(TPL)ThreadPool中新创建的线程中的异常不被其他线程捕获或如上述链接中所述:
While unobserved exceptions will still cause the
UnobservedTaskException event to be raised (not doing so would be a
breaking change), the process will not crash by default. Rather, the
exception will end up getting eaten after the event is raised,
regardless of whether an event handler observes the exception.
这意味着当使用WaitAny()时,如果其中一个任务完成而没有任何异常,则不会捕获其他任务中的异常.
内容总结
以上是互联网集市为您收集整理的c# – WaitAll vs WaitAny全部内容,希望文章能够帮你解决c# – WaitAll vs WaitAny所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。