Parallel.For步长

有谁知道是否有任何重载允许我在Parallel.For循环中指定步长? c#或VB.Net中的样本会很棒。

谢谢,贡萨洛

谷歌的“enumerable.range步骤”,你应该能够实现提供步进范围的Enumerable.Range的替代实现。 然后你可以做一个

Parallel.ForEach(BetterEnumerable.SteppedRange(fromInclusive, toExclusive, step), ...) 

如果google不能正常工作,那么实现应该是这样的:

 public static class BetterEnumerable { public static IEnumerable SteppedRange(int fromInclusive, int toExclusive, int step) { for (var i = fromInclusive; i < toExclusive; i += step) { yield return i; } } } 

或者,如果“收益率回报”给出一个heebie jeebies,您可以随时创建一个常规的旧列表:

 var list = new List(); for (int i = fromInclusive; i < toExclusive; i += step) { list.Add(i); } Parallel.ForEach(list, ...); 

如果这是一个要求,这应该很容易转换为VB。