Thursday, 5 September 2013

What happens when a Dispatcher created on a background thread is not shutdown? How to make sure that a dispatcher is properly shut down?

What happens when a Dispatcher created on a background thread is not
shutdown? How to make sure that a dispatcher is properly shut down?

The following is one of the remarks on Dispatcher class.
If you create a Dispatcher on a background thread, be sure to shut down
the dispatcher before exiting the thread.
What are the consequences if one fails to invoke shut down on a dispatcher
created on a background thread?
I have an MFC application which creates a WPF window on a background
thread. Consequently, a dispatcher is created. When I close the WPF window
first, I get to explicitly invoke shutdown on the dispatcher, but when I
close the MFC application, the WPF window closes along.
It seems the dispatcher is being shut down implicitly, or the thread is
being aborted. Which is it?
Update:
The following method creates a new thread and opens the wpf window.
public void ShowWindow(SomeObject someObject)
{
System.Threading.Thread thread = new System.Threading.Thread((tuple) =>
{
Tuple<Dispatcher, SomeObject> data = tuple as
Tuple<Dispatcher, SomeObject>;
Window window = new WPFWindow(data.Item1, data.Item2);
System.Windows.Threading.Dispatcher.Run();
this.tmp = 0;
});
thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.IsBackground = true;
thread.Start(new Tuple<Dispatcher,
SomeObject>(Dispatcher.CurrentDispatcher, someObject));
}
So, I put a break along the statement "this.tmp = 0;" and it doesn't get
hit when I close the MFC application. Is it safe to assume that the
Dispatcher is not being shutdown, but the thread is being aborted?
If the thread is aborted, what are the consequences?

No comments:

Post a Comment