简体   繁体   中英

Performance issue when launching WPF application

I have a complex WPF application that is using a lot of resources from the shared Resource Dictionary. The first Window initialized takes 8 seconds to initialize. The performance issue is less on SSD disk drives but still it requires 2 seconds.

I tried to use the Visual Studio Profiler and it shows big expense of time on InitializeComponent(); of the windows that needs to be displayed.

I believe it is related to the Resource dictionary used but I can't replace it because I really need it and because all windows and WPF elements are using the StaticResource references.

I tried to optimize the launch as much as it is possible. I created many background threads but this didn't helped too much. Whenever a window needs to be displayed it must be attached on UI thread under the same Dispatcher. This makes a big performance issue and all UI and any Progress bar left on the screen is blocked.

So to summarize. From the point when the ShowDialog is called until the window is displayed it takes 8 seconds. This is visible only on the first window. Any other window opened after that is displayed quickly.

Now I am asking firstly what happens in the background and why this delay is so big and second what can be done to increase the startup speed.

I didn't mentioned but there are no Exceptions or DataErrors present during the launch so it is not related to Exceptions.

I believe it is something with the initialization of Buttons and other components because almost all of them have the ControlTemplate restyled.

Lots of assemblies need to be loaded and lots of code must be JIT compiled before your first window can be shown. One useful technique to reduce startup time is to structure your code in such a way that types are not loaded before they are needed. It may be preferable to get a blank window up on screen with a wait indicator before delving into code outside of the core WPF assemblies. Optimize for that scenario.

Avoid loading images/media and other resources too early if you are trying to get something up on the screen as soon as possible.

Avoid loading any data synchronously, and do as little in your view and view model constructors as possible. Defer the loading of data until your view has been shown (throw up a wait indicator if necessary).

If you think your Xaml resources are a problem, split them up, and have each view pull in only the resources it needs. Don't merge them into App.xaml . You might also look into how to share the resources more efficiently across multiple views.

Throwing up a splash screen can improve the perceived startup time. Getting anything up on the screen to let the user know your app is actually doing something goes a long way.

Lastly, don't fret too much; poor startup time is the hallmark of WPF applications, and in the end, there's only so much you can do.

You can also use the ProfileOptimization class to improve startup time over subsequent sessions of the program. May not help you, the developer, but may have an impact on your users for the better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM