Wpf Dispatcherbegin Invoke to Continue Loading Screen

Launching a WPF Window in a Separate Thread, Part 1

Typically, I strongly recommend keeping the user interface within an application’s main thread, and using multiple threads to move the actual “work” into background threads.  However, there are rare times when creating a separate, dedicated thread for a Window can be beneficial.  This is even acknowledged in the MSDN samples, such as the Multiple Windows, Multiple Threads* sample.  However, doing this correctly is difficult.  Even the referenced MSDN sample has major flaws, and will fail horribly in certain scenarios.  To ease this, I wrote a small class that alleviates some of the difficulties involved.

The MSDN Multiple Windows, Multiple Threads Sample* showed how to launch a new thread with a WPF Window, and will work in most cases.  The sample code (commented and slightly modified) works out to the following:

          // Create a thread          Thread newWindowThread =          new          Thread(new          ThreadStart( () => {          // Create and show the Window          Window1 tempWindow =          new          Window1();     tempWindow.Show();          // Start the Dispatcher Processing          System.Windows.Threading.Dispatcher.Run(); }));          // Set the apartment state          newWindowThread.SetApartmentState(ApartmentState.STA);          // Make the thread a background thread          newWindowThread.IsBackground =          true;          // Start the thread          newWindowThread.Start();

This sample creates a thread, marks it as single threaded apartment state, and starts the Dispatcher on that thread. That is the minimum requirements to get a Window displaying and handling messages correctly, but, unfortunately, has some serious flaws.

*Note: The referenced sample is no longer available as of 2013. A new Multithreaded Web Browser Sample shows a similar technique and code

The first issue â€" the created thread will run continuously until the application shuts down, given the code in the sample.  The problem is that the ThreadStart delegate used ends with running the Dispatcher.  However, nothing ever stops the Dispatcher processing.  The thread was created as a Background thread, which prevents it from keeping the application alive, but the Dispatcher will continue to pump dispatcher frames until the application shuts down.

In order to fix this, we need to call Dispatcher.InvokeShutdown after the Window is closed.  This would require modifying the above sample to subscribe to the Window’s Closed event, and, at that point, shutdown the Dispatcher:

          // Create a thread          Thread newWindowThread =          new          Thread(new          ThreadStart( () => {     Window1 tempWindow =          new          Window1();          // When the window closes, shut down the dispatcher          tempWindow.Closed += (s,e) =>         Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);      tempWindow.Show();          // Start the Dispatcher Processing          System.Windows.Threading.Dispatcher.Run(); }));          // Setup and start thread as before        

This eliminates the first issue.  Now, when the Window is closed, the new thread’s Dispatcher will shut itself down, which in turn will cause the thread to complete.

The above code will work correctly for most situations.  However, there is still a potential problem which could arise depending on the content of the Window1 class.  This is particularly nasty, as the code could easily work for most windows, but fail on others.

The problem is, at the point where the Window is constructed, there is no active SynchronizationContext.  This is unlikely to be a problem in most cases, but is an absolute requirement if there is code within the constructor of Window1 which relies on a context being in place.

While this sounds like an edge case, it’s fairly common.  For example, if a BackgroundWorker is started within the constructor, or a TaskScheduler is built using TaskScheduler.FromCurrentSynchronizationContext() with the expectation of synchronizing work to the UI thread, an exception will be raised at some point.  Both of these classes rely on the existence of a proper context being installed to SynchronizationContext.Current, which happens automatically, but not until Dispatcher.Run is called.  In the above case, SynchronizationContext.Current will return null during the Window’s construction, which can cause exceptions to occur or unexpected behavior.

Luckily, this is fairly easy to correct.  We need to do three things, in order, prior to creating our Window:

  • Create and initialize the Dispatcher for the new thread manually
  • Create a synchronization context for the thread which uses the Dispatcher
  • Install the synchronization context

Creating the Dispatcher is quite simple â€" The Dispatcher.CurrentDispatcher property gets the current thread’s Dispatcher and “creates a new Dispatcher if one is not already associated with the thread.”  Once we have the correct Dispatcher, we can create a SynchronizationContext which uses the dispatcher by creating a DispatcherSynchronizationContext.  Finally, this synchronization context can be installed as the current thread’s context via SynchronizationContext.SetSynchronizationContext.  These three steps can easily be added to the above via a single line of code:

          // Create a thread          Thread newWindowThread =          new          Thread(new          ThreadStart( () => {          // Create our context, and install it:          SynchronizationContext.SetSynchronizationContext(          new          DispatcherSynchronizationContext(             Dispatcher.CurrentDispatcher));      Window1 tempWindow =          new          Window1();          // When the window closes, shut down the dispatcher          tempWindow.Closed += (s,e) =>         Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);      tempWindow.Show();          // Start the Dispatcher Processing          System.Windows.Threading.Dispatcher.Run(); }));          // Setup and start thread as before        

This now forces the synchronization context to be in place before the Window is created and correctly shuts down the Dispatcher when the window closes.

However, there are quite a few steps.  In my next post, I’ll show how to make this operation more reusable by creating a class with a far simpler API…

About Reed
Reed Copsey, Jr. - http://www.reedcopsey.com - http://twitter.com/ReedCopsey

oxleyfellnisomer.blogspot.com

Source: http://reedcopsey.com/2011/11/28/launching-a-wpf-window-in-a-separate-thread-part-1/

0 Response to "Wpf Dispatcherbegin Invoke to Continue Loading Screen"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel