Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create and show WPF windows on separate threads?

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?

like image 893
Mikael Sundberg Avatar asked Jul 10 '09 18:07

Mikael Sundberg


People also ask

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.

Is WPF multi threaded?

WPF supports a single-threaded apartment model that has the following rules: One thread runs in the entire application and owns all the WPF objects. WPF elements have thread affinity, in other words other threads can't interact with each other.

How do I navigate between windows in WPF?

In this article To package content for navigation, WPF provides the Page class. You can navigate from one Page to another declaratively, by using a Hyperlink, or programmatically, by using the NavigationService.

What is the difference between a page and a window in WPF when you are adding a new file in the Solution Explorer?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.


1 Answers

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

like image 84
Kenan E. K. Avatar answered Oct 05 '22 23:10

Kenan E. K.