开发者

Is Graphics.DrawImage asynchronous?

开发者 https://www.devze.com 2023-01-03 14:19 出处:网络
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.

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(_wid开发者_Go百科th, _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.

Edit:

Nevermind, it happens also when the methods get executed in the same thread..


Yeah, is synchronous. But you make bigger assumptions in this code, you assuming that creating any Graphics object is thread-safe. Afaik it is on the desktop version of GDI+. It wouldn't be so likely on a limited resource OS like WM. Nothing you can lock, the one used for painting is created in code you can't touch.


Well, DrawImage isn't async. The framework wouldn't automatically make it async. Also, most all async operations in .NET start with 'Begin' just fyi.

I'm not sure where your error is coming from, but can you:

  • tell us what line the AccessViolationException is throw from?
  • make sure _bitmapSyncRoot is initialized?
0

精彩评论

暂无评论...
验证码 换一张
取 消