Hands-on experiment
Experiment 5: Process life cycle Management
September 2012
Brief introduction
Process lifecycle Management is one of the most important concepts that need to be understood by developers who build Windows application store applications. Unlike traditional Windows applications, which continue to execute even in the background, Windows store applications are only performed at the foreground. Applications that are not normally visible are suspended by the operating system and cannot be performed until the operating system restores it to the foreground.
When the application is suspended, it is still in memory, and all its threads are suspended. As long as the process is still in memory, it will continue from where it stopped when the application returns to the foreground. Developers do not need to do any work to do this. However, you cannot be guaranteed that the process is always in memory. If the Windows kernel decides that the memory used by the application needs to be suspended when the application is suspended, Windows can terminate the pending application. When an application is terminated, all unsaved states are lost. Unless you take steps to maintain state, it would be surprising if the user returned to the application and found that all completed work was lost.
Obviously we won't allow it to happen. When an application is suspended, even Windows does not know whether the application will eventually be terminated. You should assume that the application will be terminated, so you must write code to save the state of the application when it is suspended. Then, if you detect that a termination occurs when the application is suspended, you can recover the status when you reactivate.
Sounds a lot of trouble? Sometimes, but not for most applications, and this is what you have to do to write good applications. The potential cost of not doing this is that simply switching to another application loses all the work he or she does in your application.
Goal
This experiment will introduce you to process lifecycle management and show you some of the code that Visual Studio wrote for you in the default template. Because the Contoso recipe is a simple application, we don't need to write new code to handle process lifecycle management, and Visual studio injects enough code. You may have to write more code for your application, so we will tell you where the code is and what application interfaces (APIs) you can use. Stop typing and sit down to have a rest, but be sure to concentrate on your attention.
System Requirements
You need the following software to complete this experiment:
Microsoft Windows 8
Microsoft Visual Studio 2012
Set up
You must perform the following steps to prepare the computer for this experiment:
1. Install Microsoft Windows 8.
2. Install Microsoft Visual Studio 2012.
Practice
This hands-on experiment includes the following exercises:
1. Navigation status has been saved
2. Tips
Exercise 1: The navigation state has been saved
Because the only state in the Contoso cookbook that needs to be saved is the navigation state (that is, the item or group viewed by the user and navigation history), you do not need to do any work for process lifecycle management. Visual Studio contains a class named Suspensionmanager in your application that is located in the SuspensionManager.cs of the project Common folder. Visual Studio also contains a line of code in the app constructor in App.xaml.cs that registers an event handler for the suspending event. As shown here, the handler onsuspending calls Suspensionmanager.saveasync to save the application's navigational state.
Private async void Onsuspending (object sender, Suspendingeventargs e)
{
var deferral = e.suspendingoperation.getdeferral ();
await Suspensionmanager.saveasync ();
Deferral.complete ();
}
Visual Studio includes an IF clause in the Onlaunched event of the App.xaml.cs file that restores the application's navigational state if it is terminated by the operating system after it is suspended.
if (args. Previousexecutionstate = = applicationexecutionstate.terminated)
{
Restore the saved session state when appropriate
await Suspensionmanager.restoreasync ();
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/