Is Graphics.DrawImage asynchronous?

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);
   // Draw bitmap //

   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.

+3
source share
2 answers

, DrawImage . . , .NET "Begin" fyi.

, , :

  • , AccessViolationException?
  • , _bitmapSyncRoot?
0

, . , , Graphics . Afaik - GDI+. , WM. , , , , , .

+1

Source: https://habr.com/ru/post/1749892/


All Articles