WPF is based heavily on
DependencyObject
s, which can only be hosted in a STA thread. So creating them in a non-STA thread results in exceptions. On top of that, those
BackgroundWorder
s are there for doing background work, if you have a long running process that won't let go of the thread, use a
BackgroundWorker
. They're not intended to play host to WPF controls.
So, if you want to create a new
Window
outside of the UI thread, you need to create a new
Thread
(not thread) and call the
SetApartmentState
method with a
ApartmentState.STA
value for the parameter.
Thread otherWindowHostingThread = new Thread(new ThreadStart(OtherThreadStartPoint));
otherWindowHostingThread.SetApartmentState(ApartmentState.STA);
otherWindowHostingThread.Start();
The method you use as your thread starting point needs to call the new
Window
's
Show()
method, and it should also tell the that thread's
Dispatcher
to start before the method finishes executing.
I'm not 100% sure how well this will work out, because I've only done this when I was first leaning WPF on .net 3.5, and the only reason I did it was because I was learning WPF. I should also warn you that, any
Window
s created outside of the
App
's UI thread will not be inserted in the
App
's
Window
s collection property.
A full solution to this can be found in chapter 8 section 16 of "WPF Recipes in C# 2008, A Problem-Solution Approach" by Sam Noble, Sam Bourton, and Allen Jones.