Best practices for your app's startup performance (Windows Store apps using C#/VB/C++ and XAML)
Users perceive whether your app is fast or slow, in part, by on how long it takes the app to start. For the purposes of this topic, an app's startup time begins when the user starts the app and ends when the user can interact with the app in some meaningful way. This topic provides suggestions on how to get better performance of your app when it starts.
Be sure to start your app a few times before you actually measure its startup time. This gives you a baseline for your measurement and ensures that your app is started as quickly as possible.
It's a good idea to run the Native Image Generator (Ngen.exe) tool to precompile your app before you measure its startup time. By default, Windows 8 automatically precompiles Windows Store apps by running a scheduled task, so your users will get the benefit of your app running as quickly as possible. You should run Ngen.exe to get measurements that are representative of what the end user will experience.
The following procedure describes how to run Ngen.exe to precompile your app.
To run Ngen.exe
- Run your app at least one time to ensure that Ngen.exe detects it.
- Open the Task Scheduler by doing one of the following:
- Search for "Task Scheduler" from the start screen.
- Run "taskschd.msc."
- In the left-hand pane of Task Scheduler, expand Task Scheduler Library.
- Expand Microsoft.
- Expand Windows.
- Select .NET Framework.
Select .NET Framework NGEN 4.x from the task list.
If you are using a 64-bit computer, there is also a .NET Framework NGEN v4.x 64. If you are building a 64-bit app, select .NET Framework NGEN v4.x 64.
- From the Action menu, click Run.
Ngen.exe precompiles all the apps on the machine that have been used and do not have native images. If there are a lot of apps that need to be precompiled, this can take a long time, but subsequent runs are much faster.
When you recompile your app, the native image is no longer used. Instead, the app is just-in-time compiled, which means that it is compiled as the app runs. You must rerun Ngen.exe to get a new native image.
To increase your app's startup time, do only the work that absolutely needs to be done to let the user start interacting with the app. This can be especially beneficial if you can delay loading additional assemblies. The common language runtime loads an assembly the first time it is used. If you can minimize the number of assemblies that are loaded, you might be able to improve your app's startup time and its memory consumption.
Your app can be interactive even though there are parts of the app that aren't fully functional. For example, if your app displays data that takes a while to retrieve, you can make that code execute independently of the app's startup code by retrieving the data asynchronously. When the data is available, populate the app's user interface with the data.
Many of the Windows Runtime APIs that retrieve data are asynchronous, so you will probably be retrieving data asynchronously anyway. For more info about asynchronous APIs, see Quickstart: Using the await operator for asynchronous programming. If you do work that doesn't use asynchronous APIs, you can use the Task class to do long running work so that you don't block the user from interacting with the app. This will keep your app responsive to the user while the data loads.
If your app takes an especially long time to load part of its screen, consider adding a string in that area that says something like, "Getting latest data," so that your users know that the app is still working.
Build date: 11/16/2013