I have a large WPF MVVM application (over 100 windows currently and growing.) Although I try to do everything I can on background threads there always comes a time the results must be sent back to the UI thread to be displayed. When you have many windows doing this at the same time it can effect performance.
I've tried to run each window on a separate UI thread in the past but ran into so many threading issues I had to revert back to WPF's default model of only 1 UI thread per app.
I know with windows 10 coming many users will open even more windows on separate desktops and thus make this worse.
Anyone know how to get multiple UI threads to work correctly in WPF? Or have any info I can investigate to help get my app further down this road?
I approach I tried in the past was to do something similar to this:
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();
}
This was about 2 years ago and I can no longer recall all the issues I faced using this unfortunately.
Is there another way? Has anyone gotten an app with many windows to work correctly using this approach or another?
When you have many windows doing this at the same time it can effect performance- so you're doing 100 windows simultaneously? I seriously doubt your end user is able to handle that.