I'm just wondering, asynchronously Graphics.DrawImage()? I am struggling with a thread safety issue and can't figure out where the problem is.
if I use the following code in a GUI thread:
protected override void OnPaint(PaintEventArgs e)
{
lock (_bitmapSyncRoot)
{
e.Graphics.DrawImage(_bitmap, _xPos, _yPos);
}
}
And add the following code to a separate thread:
private void RedrawBitmapThread()
{
Bitmap newBitmap = new Bitmap(_width, _height);
Bitmap oldBitmap = null;
lock (_bitmapSyncRoot)
{
oldBitmap = _bitmap;
_bitmap = newBitmap;
}
if (oldBitmap != null)
{
oldBitmap.Dispose();
}
Invoke(Invalidate);
}
Could this explain the accessviolation exception?
The code runs on a Windows Mobile 6.1 device with a compact 3.5 framework.
Edit:
Nevermind, this also happens when methods are executed on the same thread.
source
share