Parallel.For步长

Gon*_*alo 8 .net c# vb.net parallel-extensions

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

谢谢,贡萨洛

Dax*_*ohl 15

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

Parallel.ForEach(BetterEnumerable.SteppedRange(fromInclusive, toExclusive, step), ...)
Run Code Online (Sandbox Code Playgroud)

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

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

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

var list = new List<int>();
for (int i = fromInclusive; i < toExclusive; i += step) {
    list.Add(i);
}
Parallel.ForEach(list, ...);
Run Code Online (Sandbox Code Playgroud)

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