Click here to Skip to main content
15,669,040 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a style loaded wpf application and I need to create a separate UI thread for a window. The window uses styles that the rest of the application is using. The styles is in a resource xaml embedded in the solution. The window is created with code that looks like this.
Dim thread As New Thread(Sub()
                             Dim w As New TimelineSceneEd

                         End Sub)

When the window calls InitializeCompenent() I get the following exception

'Set property 'System.Windows.FrameworkElement.Style' threw an exception.' Line number '27' and line position '171'.

with the inner exception of:
{"The calling thread cannot access this object because a different thread owns it."}

My guess is that there is a generated class somewhere that is used to parse the xaml file containing the styles for the wpf program, and that this class is on the other main UI Thread. However if i run a dispatcher.invoke off to the main ui thread with initlializecompenent, I still get problems. Any ideas?

Of course I cannot look at any source code because this is framework code.
Updated 4-Dec-13 20:57pm

There is no such thing as "multiple UI threads". You can only ever have one, the thread that's created when your application starts. That is the only thread that can touch the user interface and its controls.

In your case, you use Invoke from a second thread to tell the UI thread to run code, not the other way around.
Share this answer
Ron Beyer 5-Dec-13 9:52am    
+5, WPF, despite its heavy dependence on DirectX, is still a single threaded UI.
Jacob Himes 5-Dec-13 10:14am    
Ron Beyer 5-Dec-13 10:17am    
That link only shows that work can be offloaded from the UI thread to other threads, it still stands that the UI thread is the only UI thread, you cannot create multiple UI threads. Anything dealing with the UI must be Invoked back to the UI thread or you will get a runtime error.
Jacob Himes 5-Dec-13 10:28am    
I believe you are scheming the article. The article states that there is underlying rendering thread, and then whats called the "UI Thread", a ui thread has its own dispatcher, and you can create multiple UI threads all with their own dispatchers.

Here is another quick article I found of someone who had a question on multiple UI threads
Jacob Himes 5-Dec-13 10:51am    
Mechanically speaking, you are right, work is being offloaded to a different thread, but the UI thread you are talking about is the rendering thread. There is a one and only "User Interface Thread" but its of lower abstraction and called the rendering thread.
Why would you need multiple threads on UI, especially on WPF, it uses a Thread pool to dispatch things that it have to do.
Share this answer
Jacob Himes 5-Dec-13 10:22am    
I've read that a multi-threaded UI lessens the load, I'm very familiar with dispatching and such. I have a lot of real-time updating going on, and I want separate dispatchers running on separate threads for unrelated windows. I've read in msdn that the thread pool bottles down still to the message pump that is on the UI thread, so a single thread is doing the work, with a rendering thread beneath it. If you believe I'm wrong, please respond with citations.
Luiz Monad 5-Dec-13 11:01am    
That's a good answer. If you are using one thread per root UI component for unrelated windows, then its a good way to lessen the load without threading problems (just like STA thread and normal win32 UI with messagepump for each thread).
There should be no problems if the UI root and all it's children are on the same thread, but you have more UIs with other root on another thread.
I would suspect the style problem comes from the way the Initializer creates the components. I think the style can't be shared between threads.
Perhaps if you use System.Windows.Markup.XamlReader.Load to create the UI from the correct thread, it would not share the style and cause problems.
Jacob Himes 5-Dec-13 14:11pm    
I think at this point, I'm going to shy away from multi-threading dispatchers. I'll accept your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900