【发布时间】:2009-10-12 03:53:27
【问题描述】:
我正在使用通过多个线程调用主函数的 API。我试图通过该函数访问另一个类中的位图并从中写入,但即使在将其设置为使用完全不同的对象实例后,我仍遇到 InvalidOperationException:位图区域已锁定。
我已经尝试在 main 函数和调用 Bitmap.LockBits(...) 的位置锁定代码。是的,我完成后正在调用 UnlockBits。
/* Part of Class B */
public Surface imageSurface //Surface is a field of pixels, more or less.
{
get
{
if (_CurrImage != null && _imageSurface == null)
{
_imageSurface = Surface.CopyFromBitmap(_CurrImage);
return Surface.CopyFromBitmap(_imageSurface.CreateAliasedBitmap());
}
else
{
Surface clearPixel = new Surface(1, 1);
clearPixel[0, 0] = ColorBgra.FromBgra(255, 255, 255, 0);
return clearPixel;
}
}
}
/* the "main" function, Class A */
public override void Render(ClassBPrototype parameters, ...)
{
ClassB token = (ClassB)parameters; // Here we go again~!
...
Surface ourSurface = dstArgs.Surface;
if (token.imageSurface.Size != null)
{
ourSurface = token.imageSurface;
}
lock(typeof(ClassA))
{
for (int lRectangleIndex = ...)
{
Rectangle lRectangle = rois[lRectangleIndex];
for (int y = ...)
{
surfaceY = (ourSurface.Height / 2) - (y - (int)CenterY);
for (int x = ...)
{
surfaceX = (ourSurface.Width / 2) - (x - (int)CenterX);
if (surfaceX >= 0 && surfaceX < ourSurface.Width && surfaceY >= 0 && surfaceY < ourSurface.Height)
{
dstArgs.Surface[x, y] = ourSurface[surfaceX, surfaceY];
}
else
{
dstArgs.Surface[x, y] = ColorBgra.FromBgra(255, 255, 255, 0);
}
}
}
}
}
}
【问题讨论】:
-
请发布一个小而完整的示例来演示该问题。
-
@Andrew:我的想法完全正确——“完全不同的实例”的定义听起来很关键。
-
它可能不相关,但对我来说看起来不合适。您不应该锁定 (typeof(ClassA))。我通常锁定一个私有变量。请参阅此处了解更多详情msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.80).aspx
标签: c# .net multithreading