views:

262

answers:

2

Hello,

I need to create two (or more) WPF windows from the same process. But the windows must be handled by separate threads because they should not be able to block each other. How do I do this?

In WinForms this is achieved by:

  • Start a new thread
  • Create a form from the new thread
  • Call Application.Run with the form as parameter

But how do I do the same in WPF?

+2  A: 

As msdn states:

private void NewWindowHandler(object sender, RoutedEventArgs e)
{       
    Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
    newWindowThread.SetApartmentState(ApartmentState.STA);
    newWindowThread.IsBackground = true;
    newWindowThread.Start();
}

private void ThreadStartingPoint()
{
    Window1 tempWindow = new Window1();
    tempWindow.Show();       
    System.Windows.Threading.Dispatcher.Run();
}
kek444
A: 

I think I found the answer. Look at Jon Skeet's answer in this question.

Basically you do like this in your thread start method:

private void ThreadStartingPoint()
{
    Window1 tempWindow = new Window1();
    tempWindow.Show();       
    System.Windows.Threading.Dispatcher.Run();
}
Mikael Sundberg
Ooops.. to late, thank you kek444.
Mikael Sundberg