Reputation: 167
I work on an application which has a main window and some TextBlocks on it. Once user clicks a TextBlock, a new window is created and display the data chart related to that TextBlock. I create such new chart Wndow on a different thread, as following:
private void xTB_MouseDown(object sender, MouseButtonEventArgs e)
{
Thread _displayChartThread = new Thread(() =>
{
Window w = new Window()
w.Closed += (sender2, e2) =>w.Dispatcher.InvokeShutdown();
w.Show();
System.Windows.Threading.Dispatcher.Run();
});
_displayChartThread.SetApartmentState(ApartmentState.STA);
_displayChartThread.IsBackground = true;
_displayChartThread.Start();
}
The problem is, when you have multiple chart windows open, and you close the main window, the chart windows are still open and not get destroyed. I can see that the chart windows have no knowledge of the main window being their Owner, and I can't set their Owner property to main window in the thread in which chart window is created.
Any ideas on how to handle this? I mean once main window is closed, all the chart window (created in different threads) are also closed.
Thanks in advance.
Upvotes: 2
Views: 968
Reputation: 24383
You can keep references to the child windows in the main window. When the main window is closed, you can close all the child windows ( invoke CLose
using the childWindow.Dispatcher
)
Upvotes: 1