此属性不是
public
。如果您对当前迭代感兴趣,唯一的选择是将其作为数据的一部分传递:
var indexedSource = source.Select((item, index) => (item, index));
Parallel.ForEach(indexedSource, parallelOptions, entry =>
{
var (item, index) = entry;
// Process the item, that has this index.
});
Select
LINQ 运算符,其 selector
类型为 Func<TSource, int, TResult>
。
ParallelLoopState
不会公开此成员,您可以使用接受 Parallel.ForEach
:的
Action<TSource,ParallelLoopState,Int64>
重载
它提供以下参数:当前元素、
和当前元素的索引(ParallelLoopState
)。Int64
Parallel.ForEach(collection, (el, _, index) => ...);
另一个选项是使用
Parallel.For
(如果源集合允许通过索引访问):
Parallel.For(0, collection.Length, index =>
{
var current = collection[index];
});