One of the nicest improvements in the Windows Phone 7 (WP7) Mango release is fast app switching (FAS), which most folks will call multitasking. While Mango does support background execution of Tasks, the FAS system is not multitasking, because the application does not continue to execute code while it is not in the foreground.

Last year, I wrote about tombstoning in WP7, and that information still applies. Tombstoning still exists in WP7, and you should take advantage of that in your applications. With FAS, though, tombstoning occurs far less frequently. Instead, in situations where your application is deactivated (such as using the Search button on the phone to do an Internet search while you were in the middle of work), the application is put into a dormant state instead of being tombstoned. Once put in this dormant state, the application’s state, memory, etc. is completely preserved; it is essentially a freezing of the thread. If the OS determines that it is running low on RAM, it may take some dormant applications and tombstone them, which is why it is important that you continue to write code for tombstoning. The big advantage with FAS is that your application does not need to restore state from storage, which can save a lot of time, not show the Resuming screen, and so on.

Like the tombstoning model, returning from the dormant state will fire the Application_Activated event, which you can handle in App.xaml.cs. However, this is where you traditionally take care of returned from the grave in the tombstone model. Luckily for us, the ActivatedEventArgs object passed to the Application_Activated event has a new property: IsApplicationInstancePreserved. If this property is false, you are coming back from being tombstoned. If it is true, you should skip the restoration of state since it was already done for you. You will need to make sure that your handling of activation handles these items properly.

If the IsApplicationInstancePreserved property is false, though, you should continue with your normal restoration from tombstoning. Remember, you have no choice about whether your application goes to the tombstone state or the dormant state, so be prepared for both. There are certain things that will not be active when you return from the dormant state, such as any network sockets or timers. To test your tombstone code, go to your project’s properties and look on the Debug tab, and you will see a setting that lets you make sure the application always goes to the tombstone state when deactivated.

Overall, working with the FAS system is about as easy as it get. Again, do not ignore tombstoning simply because you are using it, but see it as a way of having the state restored quickly without needing to read a ton of data out of storage or rehydrate objects, and your users will get a smooth, desktop-like app switching experience.