1

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?

Core-One
  • 466
  • 2
  • 6
  • 19

2 Answers2

3

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):

private void OpenSomeWindows()
{
    for (int i = 0; i < 3; i++)
    {
        TestWindow T = new TestWindow();
        T.Show();
    }
    Dispatcher.Run(); // <---------
}

(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:

List<TestWindow> windows = new List<TestWindow>();
for (int i = 0; i < 3; i++)
{
    TestWindow t = new TestWindow();
    t.Show();
    windows.Add(t);
}
Dispatcher.Run();

P.P.S.: Maybe you want your windows to run in the main thread? Actually you can do this. You need just the following:

private void SomeMethod_OnA_NON_STA_Thread()
{
    // some other work here
    Application.Current.Dispatcher.Invoke(OpenSomeWindows);
}

private void OpenSomeWindows()
{
    for (int i = 0; i < 3; i++)
    {
        TestWindow T = new TestWindow();
        T.Show();
    }
    // this way, no Dispatcher.Run is needed
}
Vlad
  • 35,022
  • 6
  • 77
  • 199
  • Thx!!!!! Now it works. Again I Find I have to read more on this dispatcher thing. – Core-One Jun 01 '11 at 15:39
  • 1
    This last iteration: (Application.Current.Dispatcher.Invoke(OpenSomeWindows);) is (IMHO) the best way of dealing with UI across different threads, especially if you're trying to write something quickly, and don't want to have to learn or relearn threading. I know that's bad, but for prototypes and small projects, sometimes you just want something that works, which this does. – Tom Morgan Jan 16 '14 at 19:16
0

The Thread dies at the end of the calling method. Make ANewThread into a field (declare it at the class/Form level).

Dabblernl
  • 15,831
  • 18
  • 96
  • 148