是否可以使用Parallel.For
循环绘制面板?我有一个多维数组double,double[][] plants
,我想并行绘制列,其中数组中的每个条目在面板上的网格中绘制为矩形。Windows窗体并行绘图
在有grapahics.FillRectangle()
行了,当我尝试我不断收到此错误:
“System.InvalidOperationException”类型的异常出现在System.Drawing.dll程序,但在用户代码中没有处理
附加信息:对象当前正在其他地方使用。
这里是我使用的代码:
Parallel.For(0, simWidth, i =>
{
Color plantColor;
RectangleF plantRectangle= new Rectangle();
SolidBrush plantBrush = new SolidBrush(Color.Black);
for (int j = 0; j < simHeight; ++j)
{
int r, g = 255, b;
r = b = (int)(255 * (Math.Tanh(simulation.plants[i, j]) + 1)/2.0);
plantColor = Color.FromArgb(100, r, g, b);
plantBrush.Color = plantColor;
plantRectangle.Location = new PointF(i * cellSize, j * cellSize);
graphics.FillRectangle(plantBrush, plantRectangle);
}
plantBrush.Dispose();
});
我认为正在发生的事情是,图形对象一下子无法处理多个呼叫。有没有办法解决?我试图在每次并行调用中创建一个对图形对象的本地引用,但没有奏效。
'graphics'来自'PaintEventArgs e','e.Graphics'。我正在寻求平行,因为它本质上是“呈现”我的模拟。缓存和使用“位图”听起来像是个好主意,谢谢! –