有没有人知道有没有重载可以让我在Parallel.For循环中指定步长?无论是c#还是VB.Net格式的样例都很棒。
谢谢,贡萨洛
发布于 2011-08-22 09:56:58
在谷歌上搜索"enumerable.range step“,你应该能够找到提供步进范围的Enumerable.Range的替代实现。然后,您可以只做一个
Parallel.ForEach(BetterEnumerable.SteppedRange(fromInclusive, toExclusive, step), ...)如果google不工作,实现应该是这样的:
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;
}
}
}或者,如果"yield“让人胆战心惊,你总是可以在原地创建一个常规的旧列表:
var list = new List<int>();
for (int i = fromInclusive; i < toExclusive; i += step) {
list.Add(i);
}
Parallel.ForEach(list, ...);如果需要的话,这应该可以很容易地翻译成VB。
https://stackoverflow.com/questions/7142446
复制相似问题