本文介绍了是AsParallel().ForAll可靠的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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可靠的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 19:30