Minimize startup time (XAML)
All but the simplest apps require a perceivable amount of time to load resources, parse XAML, set up data structures, and run logic at activation. Here we analyze the process of activation by breaking it into three phases. We also provide tips for reducing the time spent in each phase and techniques for making each phase of your Windows Store app using C++, C#, or Visual Basic for Windows 8 more palatable to the user.
The activation period is the time between when a user starts an app and the moment the app is functional. This is a critical time because it’s a user’s first impression of your app. They expect instant and continuous feedback from the system and apps. The system and the app are perceived to be broken or poorly designed when apps don't start quickly. Even worse, if an app takes too long to activate the Process Lifetime Manager (PLM) might kill it or the user might uninstall it.
Let’s use a simple online game to identify each phase of startup and different techniques to give the user feedback throughout the process. For the game, an example of the first phase of activation is the time between when the user taps on the app’s tile and when the game starts running app code. During this time the system doesn’t have any content to display to the user to indicate that the correct app started. Providing a splash screen gives the system this very content. The game then informs the user that the first phase of activation has completed by replacing the static splash screen with its own UI when it begins running code.
The second phase of activation encompasses creating and initializing structures critical for the app. If an app can quickly create its initial UI with the data available after the first phase of activation, the second phase is trivial and you can display the UI immediately. Otherwise we recommend that the app display a loading page while it is initialized.
What the loading page looks like is up to you and can be as simple as displaying a progress bar or progress ring. The key action here is that the app indicates it is performing tasks before becoming responsive. In the case of the game, it would like to display its initial screen but that UI requires that some images and sounds be loaded from disk into memory. These tasks take a couple of seconds so the app reassures the user that it has not hung by replacing the splash screen with a loading page, which shows a simple animation related to the theme of the game.
The third stage begins after the game has a minimal set of info to create an interactive UI, which replaces the loading page. At this point the only info available to the online game is the content that the app loaded from disk. The game can ship with enough content to create an interactive UI; but because it’s an online game it won’t be functional until it connects to the internet and downloads some additional info. Until it has all the info it needs to be functional, the user can interact with the UI and features that need additional data from the web should give some kind of feedback that content is still loading. It may take some time for an app to become fully functional, so it’s important that functionality be made available as soon as possible.
Now that we identified the three stages of activation in the online game, let’s tie them to actual code.
Before an app starts, it needs to tell the system what it wants to display as the splash screen. It does so by providing an image and background color to the SplashScreen element in an app’s manifest, as in the example. Windows displays this after the app begins activation.
<Package ...> ... <Applications> <Application ...> <VisualElements ...> ... <SplashScreen Image="Images\splashscreen.png" BackgroundColor="#000000" /> ... </VisualElements> </Application> </Applications> </Package>
For more info, see Quickstart: Adding a splash screen.
Use the app’s constructor only to initialize data structures that are critical to the app. The constructor is called only the first time the app is run and not necessarily each time the app is activated. For example, the constructor isn't called for an app that has been run, placed in the background, and then activated via the search contract.
There are a number of reasons for an app to be activated, each of which you may want to handle differently. You can override OnActivated, OnCachedFileUpdaterActivated, OnFileActivated, OnFileOpenPickerActivated, OnFileSavePickerActivated, OnLaunched, OnSearchActivated, and OnShareTargetActivated methods to handle each reason of activation. One of the things that an app must do in these methods is create a UI, assign it to Window.Content, and then call Window.Activate. At this point the splash screen is replaced by the UI that the app created. This visual could either be loading screen or the app's actual UI if enough info is available at activation to create it.
Partial Public Class App Inherits Application ' A handler for regular activation. Protected Overrides Async Sub OnLaunched(ByVal args As LaunchActivatedEventArgs) MyBase.OnLaunched(args) ' Asynchronously restore state based on generic launch. ' Create the ExtendedSplash screen which serves as a loading page while the ' reader downloads the section information. Dim eSplash As New ExtendedSplash() ' Set the content of the window to the extended splash screen. Window.Current.Content = eSplash ' Notify the Window that the process of activation is completed Window.Current.Activate() End Sub ' a different handler for activation via the search contract Protected Overrides Async Sub OnSearchActivated(ByVal args As SearchActivatedEventArgs) MyBase.OnSearchActivated(args) ' Do an asynchronous restore based on Search activation ' the rest of the code is the same as the OnLaunched method End Sub End Class Partial Friend Class ExtendedSplash Inherits Page Public Sub New() InitializeComponent() ' Downloading the data necessary for ' initial UI on a background thread. Task.Run(Sub() DownloadData()) End Sub Private Sub DownloadData() ' Download data to populate the initial UI. ' Create the first page. Dim firstPage As New MainPage() ' Add the data just downloaded to the first page ' Replace the loading page, which is currently ' set as the window’s content, with the initial UI for the app Window.Current.Content = firstPage End Sub ' Shown for demonstration purposes only. ' This is typically autogenerated by Visual Studio. Private Sub InitializeComponent() End Sub End Class
Apps that display a loading page in the activation handler begin work to create the UI in the background. After that element has been created, its FrameworkElement.Loaded event occurs. In the event handler you replace the window's content, which is currently the loading screen, with the newly created home page.
It’s critical that an app with an extended initialization period show a loading page. Aside from providing the sure feedback about the activation process, the process will be terminated if Window.Activate is not called within 15 seconds of the start of the activation process.
Partial Friend Class GameHomePage Inherits Page Public Sub New() InitializeComponent() ' add a handler to be called when the home page has been loaded AddHandler Me.Loaded, AddressOf ReaderHomePageLoaded ' load the minimal amount of image and sound data from disk necessary to create the home page. End Sub Private Sub ReaderHomePageLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs) ' set the content of the window to the home page now that it’s ready to be displayed. Window.Current.Content = Me End Sub ' Shown for demonstration purposes only. ' This is typically autogenerated by Visual Studio. Private Sub InitializeComponent() End Sub End Class
For an example of using extended splash screens, see Splash screen sample.
Just because the app displayed the UI doesn't mean it is completely ready for use. In the case of our game, the UI is displayed with placeholders for features that require data from the internet. At this point the game downloads the additional data needed to make the app fully functional and progressively enables features as data is acquired.
Sometimes much of the content needed for activation can be packaged with the app. Such is the case with a simple game. This makes the activation process quite simple. But many programs (such as news readers and photo viewers) must pull info from the web to become functional. This data can be large and take a fair amount of time to download. How the app gets this data during the activation process can have a huge impact on the perceived performance of an app.
You could display a loading page, or worse, a splash screen, for minutes if an app tried to download an entire data set it needs for functionality in phase one or two of activation. This makes an app look like it’s hung or cause it to be terminated by the system. We recommend that an app download the minimal amount of data to show an interactive UI with placeholder elements in phase 2 and then progressively load data, which replaces the placeholder elements, in phase 3. For more info on dealing with data, see the section on efficiently loading, storing, and displaying data.
How exactly an app reacts to each phase of startup is completely up to you, but providing the user as much feedback as possible (splash screen, loading screen, UI while data loads) makes the user feel as though an app, and the system as a whole, are fast.
Reusable code often comes in the form of modules (DLLs) included in a project. Loading these modules requires accessing the disk, and as you can imagine, the cost of doing so can add up. This has the greatest impact on cold startup, but it can have an impact on warm startup, too. In the case of C# and Visual Basic, the CLR tries to delay that cost as much as possible by loading assemblies on demand. That is, the CLR doesn’t load a module until an executed method references it. So, reference only assemblies that are necessary to the launch of your app in startup code so that the CLR doesn’t load unnecessary modules. If you have unused code paths in your startup path that have unnecessary references, you can move these code paths to other methods to avoid the unnecessary loads.
Another way to reduce module loads is to combine your app modules. Loading one large assembly typically takes less time than loading two small ones. This is not always possible, and you should combine modules only if it doesn't make a material difference to developer productivity or code reusability. You can use tools such as PerfView or the Windows Performance Analyzer (WPA) to find out what modules are loaded on startup.
You can dramatically improve the loading time of an app by packaging its contents locally, including XAML, images, and any other files important to the app. Disk operations are faster than network operations. If an app needs a particular file at initialization, you can reduce the overall startup time by loading it from disk instead of retrieving it from a remote server.
An app written in C# or Visual Basic has different startup performance at first install and at steady state. When your app is first installed onto a user’s machine, it is executed using the CLR’s just-in-time (JIT) compiler. This means that the first time a method is executed it has to wait to be compiled. Later, a pre-compilation service (NGEN) pre-compiles all of the modules that have been loaded on a user’s machine. This typically runs within 24 hours of installation, but it could take longer depending on the usage patterns of the machine. After this service has run most methods no longer need to be JIT compiled, and your app benefits from improved startup performance. There can still be some methods that need to be JIT compiled at runtime, but there will be many fewer after the NGEN service has run.
If you want to measure the startup performance of your app, then measure with both JIT and NGEN. You can run the NGEN task using Task Scheduler by entering the following at the command line:
schTasks /run /Tn "\Microsoft\Windows\.NET Framework\.NET Framework NGEN v4.0.30319
This pre-compiles any apps that ran at least once and are missing NGEN images, and may take a long time the first time it is run.