我正在改进我创建的 3d 引擎的性能,引入 LockBits 和并行处理。我有一个引擎类,它使用以下方法更新位图的结果:
public void draw() {
clearbmp();
// locking bmp
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, W, H), ImageLockMode.ReadWrite, bmp.PixelFormat);
IntPtr FirstPixel = bitmapData.Scan0;
int bytes = Math.Abs(bitmapData.Stride) * H;
bpp = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
rgbarray = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(FirstPixel, rgbarray, 0, bytes);
int count = 0;
for (int k = 0; k < solidos.Count; k++)
{
if (solidos[k] != null && solidos[k].draw)
{
// separating faces of the solid into different threads
Parallel.ForEach(solidos[k].side, f =>
{
// ... do the operations...
});
}
}
// copy the array to the bitmap and unlock
System.Runtime.InteropServices.Marshal.Copy(rgbarray, 0, FirstPixel, bytes);
bmp.UnlockBits(bitmapData);
代码按预期运行以生成图像,但当主程序要求它快速连续更新多次时失败,错误发生在bmp.UnlockBits(bitmapData)中,异常为“GDI+ 中的通用错误”
根据我从研究中收集到的信息,我想这是因为该方法在完成第一次之前第二次运行,因此试图解锁已经解锁的数据。
如果这是正确的,那么在创建新线程时如何中止正在运行的线程?最后一个电话永远是最重要的