Skip to content

Contact Us    Now Hiring

@meritweb on TwitterMerit Solutions on LinkedInMerit Solutions YouTube Video SeriesMerit Solutions on FacebookSubscribe to Merit Matters Blog

   

Windows 8 development - Process Lifetime Management

If you are a developer, starting to work on a Windows 8 application, one of the really important things to understand is the Process Lifetime Management or PLM. Even if you're not a developer, understanding what really happens when you run a Windows Store application or navigate from it could help you use the app better.

PLM is actually one of the major differences between a Windows 8 application and a desktop application. Unlike traditional Windows applications, which will continue to execute in background, Windows Store apps execute only when they are in the foreground. Windows 8 focuses on the apps in the foreground, keeping them responsive and providing excellent performance by allowing the app to use all of the available device resources. Applications that are snapped are running in foreground, too.

Once a user navigates away from the app, the operating system puts the application in the suspended mode to preserve battery. The application remains in memory but all of its thread are suspended. When the user navigates back to the app, it resumes execution where it stopped and you as a developer can't change this behavior. You don't even have to implement any code to make it happen.

This all seems easy and straightforward, but there is a catch. The operating system cannot guarantee that the application will stay in memory until a user decides to use it again. While the app is suspended, the OS can terminate it to free up additional memory. When an app is terminated, all state that has not been saved is lost. As a developer, you cannot allow for this to happen. Since we are not aware whether the application will be terminated, we must ensure the state is saved when the app is suspended and restore this state in case termination occurs.

But do not despair, Visual Studio 2012 does much of the heavy lifting for you. If your app is not complex, the VS project template takes care of this for you, completely. In the Common folder of your Windows Store app you will find the SuspensionManager.cs. VS added the OnSuspending method and following code to existing OnLaunched method in the App.xaml.cs to save and restore the app’s navigation state if the app was terminated by the operating system after it was suspended.

private async void OnSuspending(object sender, SuspendingEventArgs e)

{

var deferral = e.SuspendingOperation.GetDeferral();
await SuspensionManager.SaveAsync();
deferral.Complete();

}

protected override async void OnLaunched(LaunchActivatedEventArgs args)

{

if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{

// Restore the saved session state only when appropriate await SuspensionManager.RestoreAsync();

}

}

In case your application requires anything more sophisticated than this, you will have to implement it yourself, but at least you have a decent starting point. To test how your application behaves when terminated, start the app from Visual Studio and select Suspend and shutdown from the Debug Location toolbar (to activate it, navigate to View > Toolbars > Debug Location).