none
GDI+ objects and multithreading

    Question

  • Sorry for duplicate, but i is not clear for me.
    1. i have N threads which use definitely different instances of Graphics (created from bitmap). Can i operate with these objects safely or i'll get some probkems???
    2. suppose that i have some render thread which has its own graphics obtained from image and sometimes i pass  tasks to this thread with instances of Brush, Pen and so on, created in different thread. Is it correct?

    PS:

    I guess that sometimes it's not good idea to pass objects which have OS handles across threads. But there is one more question: .net separates my managed thread from platform thread (other words it can be executed on different OS threads from time2time), and now how can i be shure that using the one thread in my app i'm using handles from this the one OS thread???
    Monday, August 17, 2009 5:02 PM

Answers

  • There's a lock inside GDI+ that prevents two threads from using the same Graphics object or the same Bitmap.  Whatever thread gets to it first grabs the lock, the other one will die with an exception.  GDI+ is suitable for rendering device contexts in Windows, multithreading is never an issue because device contexts are not thread safe.  Not so sure it is suitable for anything as advanced as "shadow rendering".

    Hans Passant.
    • Marked as answer by PavPS Monday, August 17, 2009 8:27 PM
    Monday, August 17, 2009 8:14 PM

All replies

  • GDI objects really should be used on the same thread as the device on which they're being drawn.  Technically, they have process affinity, so you won't have the same problems you have when using controls from multiple threads, but you do get some problems.

    This is partially why they are explicitly marked as not thread safe.  For example, the MSDN docs for the Graphics class state:

    "Any instance members are not guaranteed to be thread safe."

    I'd recommend putting the GDI operations onto your UI thread, and leaving them there.
    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, August 17, 2009 6:03 PM
  • Thanks for reply.
    But i cant understand is it safe to use simultaneously N threads rendering some tasks on separate instances of Graphics obtained from separate Images (using pens and brushes created in according threads)?
    It first look here is no prob, but readed alot about problems with this technique (maybe it's lie and all ak, but i need to know it forsure).
    Or maybe there are some best practices for realizing shadow rendering on gdi+.

    PS.
    Our  customers have hi-end multicore PCs and i need to use all it's power when rendering very complex objects, but i afraid that some mistakes in rendering architecture will crash my app.
    Monday, August 17, 2009 6:25 PM
  • There's a lock inside GDI+ that prevents two threads from using the same Graphics object or the same Bitmap.  Whatever thread gets to it first grabs the lock, the other one will die with an exception.  GDI+ is suitable for rendering device contexts in Windows, multithreading is never an issue because device contexts are not thread safe.  Not so sure it is suitable for anything as advanced as "shadow rendering".

    Hans Passant.
    • Marked as answer by PavPS Monday, August 17, 2009 8:27 PM
    Monday, August 17, 2009 8:14 PM
  • Thanks!
    PS:
    I've realized shadow rendering on bitmap tiles and the most complex rendering (rendering objects to bitmap tiles cache) are to be placed on different threads. but blitting of these tiles will occur only in ui thread. so, no multithreading with DCs.
    That's why i'm interesting in multithread bitmap rendering (one graphics per image).
    Thanks!

    Monday, August 17, 2009 8:32 PM