The following figure shows the life cycle of the WP7 program:
The sleep state and the tombstone state are confusing. The program stops running during sleep state, but the difference is that the entire process still exists in the memory. When this program is restored, you do not need to create a new instance. In this way, the program recovery and switching are accelerated, and we do not need to restore the tombstone data from the sleep state. Generally, when you click the home key, the program enters the sleep state. When the current program is running, the system will execute some operations to release the memory when the memory is insufficient or insufficient to allow the program to run smoothly, in this case, the program may change from the sleep state to the tombstone state. When a program enters the tombstone state, its process is terminated, but the information in the program's rollback stack and some information we save will be kept in the memory.
The Activated event in the Application class is triggered when the program recovers. We can check the IsApplicationInstancePreserved parameter to determine whether the program returns from the sleep or tombstone status, in this method, we can use it to restore the data previously stored in the Deactivated event.
The code is as follows:
The code is as follows: |
Copy code |
Private void Application_Activated (object sender, ActivatedEventArgs e) { If (e. IsApplicationInstancePreserved) { // Restore from sleep state } Else { // Restore the tombstone status // At this time, the program's information in the memory has been cleared, and you need to process how to restore your previous data here } } |
You can set the settings on vs to restore the tombstone status of the debugging program. The settings are as follows:
When we recover from sleep to the program, if we are sending an http request in the program, this request may be canceled, at this time, we need to capture and handle this exception in the program. The processing code is as follows:
The code is as follows: |
Copy code |
Private void GetSomeResponse (IAsyncResult MyResultAsync) { HttpWebRequest request = (HttpWebRequest) MyResultAsync. AsyncState; Try { HttpWebResponse response = (HttpWebResponse) request. EndGetResponse (MyResultAsync ); If (response. StatusCode = HttpStatusCode. OK & response. ContentLength> 0) { Using (StreamReader sr = new StreamReader (response. GetResponseStream ())) { //...... } } } Catch (WebException e) { If (e. Status = WebExceptionStatus. RequestCanceled) // It may be returned from sleep and tombstone status. // The restoration of sleep and tombstone status can be processed here, for example, re-sending an http request ...... Else { // Other exceptions Using (HttpWebResponse response = (HttpWebResponse) e. Response) { MessageBox. Show (response. StatusCode. ToString ()); } } } } |