Creating MultiThreaded UI with HostVisual
Dwayne Need has a fantastic post (with source code) on how to enable non-interactive Visual elements to run in separate threads. His summary:
In general, objects in WPF can only be accessed from the thread that created them. Sometimes this restriction is confused with the UI thread, but this is not true, and it is perfectly fine for objects to live on other threads. But it is not generally possible to create an object on one thread, and access it from another. In almost all cases this will result in an InvalidOperationException, stating that “The calling thread cannot access this object because a different thread owns it.”
[snip]
In this demo I’ve shown how to use the HostVisual and VisualTarget classes to compose pieces of UI from different threads. There are some limitations: namely that the UI owned by the worker threads do not receive input events. There were also some annoyances we had to work around along the way, but those proved to be fairly minimal.
Multithreading has been a big barrier for me, coming from the Flash world, especially since TMTOWTDI. BackgroundWorkers are very powerful and easy to get my head around, but seem to be task-oriented as opposed to long-running-process-oriented. Creating real Threads and understanding exactly what’s going on there is still a bit beyond my understanding, though. Can anyone recommend a reference that walks one through the basics of multithreading in .NET, assuming little to no knowledge to start with?
| Trackback3 Comments so far
Leave a reply


Great..
Great forum. I found exacly what I want.. But…
“But it is not generally possible to create an object on one thread, and access it from another..”
It would be very very thankful if you explain why it is so. What is reason behind it. It will help thousand of people… I guess…
Asem I.
On http://www.diranieh.com I have a long section on threading (from left-hand menu: .Net/Programming/Threading)