Multithreaded drawing in .NET?

( Edit : for clarification, my main goal is concurrency, but not necessarily for multi-core machines)

I am new to all concepts on concurrency, but I realized that I need to have parallel drawing procedures for several reasons:

  • I would like to separate the different parts of the graph separately (the background is updated less often than the foreground, it is saved in the buffer).
  • I need control over priority (more priority for responding to the user interface than drawing complex graphics).
  • I would like the frame drawing calculations to be multi-threaded.
  • I wanted to offer a cancellation for complex drawing procedures on the buffer.

However, being such a newbie, my code soon looked like a mess, and refactoring or fixing bugs became so uncomfortable that I decided that I needed to play more with it before doing anything serious.

So, I would like to know how to make clean, easy to use .NET multi-threaded code that makes sense when I look at it after waking up the next day. The biggest problem I encountered was structuring the application, so all the parts communicate with each other in a smart (as opposed to uncomfortable and hacked) way.

Any suggestion is welcome , but I prefer sources that I can digest in my free time (for example, no more than 500 pages on concurrency) and for C # / VB. NET, to the latest version (as I see it moving forward ). Basically, I want something straight to the point, so I can start by playing with the concepts of my toy projects.

+6
multithreading c # concurrency parallel-processing
source share
2 answers

A parallel task library is definitely the place to simplify your code. I personally wrote a (half-long) introduction to Parallelism with .NET 4 , which covers quite a few concepts that would be useful.

Remember, however, that you will probably want to consider whether your drawing is single-threaded. You should try to preserve the multithreading of the calculations and perform the drawing operations in the GUI thread.

Most drawing APIs require that all actual drawing calls run in the same synchronization context.

Considering that using new class classes, such as ConcurrentQueue , simplifies this type of code. Try to think in terms of multiple threads (producers), adding "drawing operations" to the overall, parallel queue - and one thread (consumer) that captures the operations and performs them.

This gives you a fairly scalable but fairly simple design on which you can create.

+1
source share

but I realized that I needed parallel drawing procedures

Three words: NOT EXPOSED TO WINDOWS.

Just. A standard drawing for one reason for each definition for compatibility reasons. Any user interface control (let it belong to the .NET world) should ONLY manipulate a creative thread out of it (so that in fact it is more cruel than single-line - it is ONLY ONE SPECIFIC THREAD).

You can do the preliminary calculation separately, but the actual drawing will be done from one thread.

If you do not select the bitmap, you have your own drawing, and then flip it to the user interface stream to draw in the window.

This has nothing to do with the entire parallel task library, etc. (which I overthrow), but brings the city back to a very old requirement, which is supported for simplicity and compatibility. It is for this reason that any stream of user interfaces must be market-based, since it has an end-to-end threaded application.

Also note that multi-threaded drawing, if you implement it yourself, has serious consequences. Which one wins optically (remains in the foreground)? This cannot be determined using multithreaded. However, you can try.

In this case:

  • Having your own buffer and synchronization is required. Stay away from any Windows-level graphics library (WPF or Winforms), with the exception of the last step (to load a bitmap).

  • DirectX 11 supposedly has some support for multi-thread calls, but I'm not sure how far this is.

+9
source share

All Articles