Is Graphics.DrawImage asynchronous?

Posted by Roy on Stack Overflow See other posts from Stack Overflow or by Roy
Published on 2010-06-14T08:16:27Z Indexed on 2010/06/14 8:22 UTC
Read the original article Hit count: 241

Hi all,

I was just wondering, is Graphics.DrawImage() asynchronous? I'm struggling with a thread safety issue and can't figure out where the problem is.

if i use the following code in the GUI thread:

protected override void OnPaint(PaintEventArgs e)
{
   lock (_bitmapSyncRoot)
   {
      e.Graphics.DrawImage(_bitmap, _xPos, _yPos);
   }
}

And have the following code in a separate thread:

private void RedrawBitmapThread()
{
   Bitmap newBitmap = new Bitmap(_width, _height);
   // Draw bitmap //

   Bitmap oldBitmap = null;
   lock (_bitmapSyncRoot)
   {
      oldBitmap = _bitmap;
      _bitmap = newBitmap;
   }
   if (oldBitmap != null)
   {
      oldBitmap.Dispose();
   }
   Invoke(Invalidate);
}

Could that explain an accessviolation exception?

The code is running on a windows mobile 6.1 device with compact framework 3.5.

© Stack Overflow or respective owner

Related posts about c#

Related posts about graphics