49

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?

Mikael Sundberg
  • 4,607
  • 4
  • 32
  • 36

4 Answers4

68

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();
}

EDIT: this IS an old answer, but since it seems to be visited often, I could also think of the following modifications/improvements (not tested).

If you would like to close such a window, simply keep a reference to the Window object from outside of the thread (delegate), and then invoke close on it, something like this:

void CloseWindowSafe(Window w)
{
    if (w.Dispatcher.CheckAccess())
        w.Close();
    else
        w.Dispatcher.Invoke(DispatcherPriority.Normal, new ThreadStart(w.Close));
}

// ...
CloseWindowSafe(tempWindow);

If the new thread could become terminated (aborted forcibly), in line with question in comments:

private void ThreadStartingPoint()
{
    try{
        Window1 tempWindow = new Window1();
        tempWindow.Show();       
        System.Windows.Threading.Dispatcher.Run();
    }
    catch(ThreadAbortException)
    {
        tempWindow.Close();
        System.Windows.Threading.Dispatcher.InvokeShutdown();
    }
    //the CLR will "rethrow" thread abort exception automatically
}

DISCLAIMER: don't do this at home, aborting threads is (almost always) against best practices. Threads should be gracefully handled via any of the various synchronization techniques, or in this case, simply via an invoked window.Close()

Kenan E. K.
  • 13,955
  • 3
  • 43
  • 48
  • 3
    Great. How can I close a window created this way? – Jakub Szułakiewicz Oct 30 '14 at 12:49
  • I know it's an old question but this approach still works. I have just one problem - i use it for loading window so - show loading window in new thread -> do stuff -> newThread.Abort() and it's ok but if i'll do it again it crashes whole app. Propably because it's going to run dispatcher again. How to resolve it ? – MajkeloDev Jul 14 '15 at 19:19
  • Hi, I was struggling with this too. For me, this [link](https://dontpaniclabs.com/blog/post/2013/11/14/dynamic-splash-screens-in-wpf/) worked. this uses the ManualResetEvent-way. There is a source to download and check too (GitHub) – dba Oct 28 '16 at 09:48
  • In my experience you cannot safely catch a `ThreadAbortException`. Best you'd remove that part of the answer. The approach with Dispatcher.Invoke + Window.Close is way safer. – sa.he Aug 24 '20 at 13:43
25

For whatever it is worth, since this answer is the first result provided by Google. I would also like to add the following taken from Eugene Prystupa's Weblog:

"There is one catch to our simplified solution. Closing a particular window does NOT terminate this window’s thread dispatcher, so the thread keeps running and, after closing all windows, the process will not terminate and will become a ghost process. [The] Simple (and not correct) solution to this is to mark our threads as background (using thread.IsBackground = true;). This will force them to terminate when main UI thread terminates.

The proper implementation will gracefully shut down the dispatcher when it is no longer needed. The code below is an example of a strategy to terminate the dispatcher when window closes:"

private void OnCreateNewWindow(object sender, RoutedEventArgs e)
{
    Thread thread = new Thread(() =>
    {
        Window1 w = new Window1();
        w.Show();

        w.Closed += (sender2, e2) =>
            w.Dispatcher.InvokeShutdown();

        System.Windows.Threading.Dispatcher.Run();
    });
        
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}
Community
  • 1
  • 1
  • 5
    Similar reference link in case the one above no longer works: [Launching a WPF Window in a Separate Thread, Part 1](http://reedcopsey.com/2011/11/28/launching-a-wpf-window-in-a-separate-thread-part-1/) – Christopher Aliotta Feb 05 '14 at 03:57
3

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();
}
Community
  • 1
  • 1
Mikael Sundberg
  • 4,607
  • 4
  • 32
  • 36
3

Exactly What I was looking for.

I am doing it like this though:

App.xaml.cs

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start();

            var window = new MainWindow(newWindowThread);
            window.DataContext = new MainWindowViewModel(window);
            window.Show();
        }

        private void ThreadStartingPoint()
        {
            SplashWindow tempWindow = new SplashWindow();
            tempWindow.Show();
            System.Windows.Threading.Dispatcher.Run();
        }
    }

MainWindow.Xaml.cs

public partial class MainWindow : Window
{
    public MainWindow(Thread splashWindowThread)
    {
        InitializeComponent();

        MyInializaComponent();

        splashWindowThread.Abort();
    }

//void DoStuff(){};
}

I needed my splash screen to go away after the program has done all of the loading.

Omzig
  • 861
  • 1
  • 12
  • 20