BioExtract
BioExtract

Reputation: 89

Open 2nd WPF window in another thread?

With the code below, I am able to generate a 2nd window but the 2nd window closes itself as soon as it initializes. What am I doing wrong here?

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Thread sta = new Thread(delegate ()
        {
            Window1 w = new Window1();
            w.Show();
        });
        sta.SetApartmentState(ApartmentState.STA);
        sta.Start();
    }

Upvotes: 3

Views: 1177

Answers (1)

BioExtract
BioExtract

Reputation: 89

Nevermind I found it! I did not put: System.Windows.Threading.Dispatcher.Run(); at the end of the deligate.

the code now reads:

private void button_Click(object sender, RoutedEventArgs e)
{
    Thread sta = new Thread(delegate ()
    {
        Window1 w = new Window1();
        w.Show();
        System.Windows.Threading.Dispatcher.Run();
    });
    sta.SetApartmentState(ApartmentState.STA);
    sta.Start();
}

Upvotes: 4

Related Questions