本文介绍了如何将 Parallel.For 转换为 PLINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Parallel.For(0, Height, new ParallelOptions { MaxDegreeOfParallelism = 6 }, y =>
{
int currentLine = y * BMPData.Stride;
for (int x = 0; x < Width; x = x + BPX)
{
var b = pixels[currentLine + x];
var g = pixels[currentLine + x + 1];
var r = pixels[currentLine + x + 2];
int avg = (r + g + b) / 3;
pixels[currentLine + x] = (byte)avg;
pixels[currentLine + x + 1] = (byte)avg;
pixels[currentLine + x + 2] = (byte)avg;
}
});
这基本上是一个并行代码,它将位图数据像素转换为灰色像素,但是有什么方法可以用并行Linq替换Parallel.For
的用法?我知道这没有意义,但这是某项任务所必需的
This is basically a parallel code where it turns bitmap data pixels into gray ones, but is there any way I can replace the Parallel.For
usage with Parallel Linq? I know there's no point to this, but it's required for a certain assignment
推荐答案
您可以使用 Enumerable.Range
和 ForAll
来获得类似的结果.尽管注意 ForAll
不会以任何稳定的顺序处理列表 - 这可能适合您的用例
You can use Enumerable.Range
, and ForAll
to get similar results. Although note ForAll
will not process the list in any stable order - which is probably fine for your use case
Enumerable.Range(0,Height)
.AsParallel()
.WithDegreeOfParallelism(6)
.ForAll(y =>
{
/// existing code here
});
这篇关于如何将 Parallel.For 转换为 PLINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!