是AsParallel().ForAll可靠的

编程入门 行业动态 更新时间:2024-10-27 15:16:28
本文介绍了是AsParallel().ForAll可靠的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用AsParallel()查询.对于我来说,ForAll有时会跳过一些记录,有时会返回空记录.

I have query with AsParallel().ForAll is sometimes skipping some records and sometimes returning null records in my case.

我不确定这是确切原因.中的域名解析机制是否可靠?

I am not sure for this is the exact reason for this. Is the mechanism for parellal for in .Net reliable?

示例代码

var collection=.. Collection from database var processedCollection=... collection.Where(h => h.Id == id).AsParallel().ForAll(h => { var processedCollectionItem = ....logic to process the item processedCollectionItem.Where(c=>c.....).AsParallel().ForAll(c => { //logic to process the records }); processedCollection.Add(processedCollectionItem); });

推荐答案

与其在并行循环中向集合中添加内容,不如让框架来处理构建集合.

Instead of adding stuff to a collection in a parallel loop, it would probably be better to let the framework handle building the collection.

示例:

var processedCollection = collection .Where(h => h.Id == id) .AsParallel() .Select(h => ProcessItem(h)) .ToArray();

在ProcessItem中,将进行处理.并行执行此操作似乎是不必要的.

In ProcessItem the processing would occur. Doing that in parallel seems unnecessary.

更多推荐

是AsParallel().ForAll可靠的

本文发布于:2023-11-12 07:06:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可靠   AsParallel   ForAll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!