Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

does anyone know if there's any overload that would allow me to specify a step size in a Parallel.For loop? Samples in either c# or VB.Net would be great.

Thanks, Gonzalo

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

Google for "enumerable.range step" and you should be able to come upon alternate implementations of Enumerable.Range that provide stepped ranges. Then you can just do a

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

If google isn't working, implementation should be something like this:

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;
        }
    }
}

Alternately if "yield return" gives one the heebie jeebies, you can always just create a regular old list in-place:

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

This should be easily translatable to VB if that's a requirement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...