I want to open some non model windows (WPF) but at the point that this has to happen I am on a non STA thread. So I start a new thread and open them on there. But as soon as the are opened they close again. (By the way. the behaviour of these windows should be independent from the mainwindow. So no owner property is set)
private void SomeMethod_OnA_NON_STA_Thread()
{
// some other work here
Thread ANewThread = new Thread(OpenSomeWindows);
ANewThread.SetApartmentState(ApartmentState.STA);
ANewThread.Start();
}
private void OpenSomeWindows()
{
TestWindow T;
for (int i = 0; i < 3; i++)
{
T = new TestWindow();
T.Show();
}
}
What am I doing wrong here?
If you want your windows to live, you have to start the message loop after you created them (otherwise your thread just exits, and the windows have no chance to render themselves):
(In main thread, the message loop is created by the framework for you.)
P.S.: I am not sure whether the windows can be garbage collected, so I would keep references to them somewhere:
P.P.S.: Maybe you want your windows to run in the main thread? Actually you can do this. You need just the following: