Multi-Tasking on Windows Phone 7 explained


Microsoft’s Peter Torr has posted what is probably the best description of how Multi-Tasking (multitastking) currently works in Windows Phone 7:

A picture tells a thousand words, and I think this one actually does a pretty good job of explaining what happens on Windows Phone 7 (but then again, maybe I’m biased). If your application is running when the user switches to another application (by using the Start menu, or tapping on a notification, or via some other means) then our assumption as a platform is that the user now wants to focus on the new application, and doesn’t want the previous one interrupting their experience by grabbing memory, CPU, network bandwidth, or other resources.

Before we suspend the application, we give it some time (exact time TBD) to prepare to be suspended. In this time, the application can save global state to disk, sign-off from web sites, or perform other clean-up operations. In general this should be relatively simple, because the page-based model of Windows Phone applications facilitates a relatively stateless programming model – much of your application’s state can be encoded in page URIs (as query-string data) or as small blobs of state stored and retrieved on each page navigation, just like the web.

After your application has finished pausing, it will be suspended and no further user code will execute. Note that you can still have push notifications coming in from the cloud, so the user can be kept up-to-date via toasts or you can have your tile updated with the latest information from the web.

When your application is suspended, it is not killed immediately. If the user returns to the application “soon” then it can be resumed very quickly and the state saved during pause may not even be necessary. But if the user launches other applications that end up needing a lot of memory, your process will be killed and the memory will be relinquished to the foreground application. This is a key difference between Windows Phone 7 and previous versions of Windows Mobile – the foreground application gets access to virtually all the resources on the phone (memory, CPU, etc.) without having to worry about being starved by background apps that are doing random things at unpredictable times in the background.

If your process was not killed, resume is trivial – you don’t need to restore any state from disk, but you may need to re-start device features like accelerometer or location, and you may need to re-connect to any web services. Assuming your process was killed, you can use the previously-saved data from pause to re-create your global state, and the per-page state / query-string data to recreate the page state for each page on the back stack.

The end result of all this is that users can switch back and forth between applications and have the illusion of multi-tasking without the downsides of erratic resource usage. Now there are certain multi-tasking scenarios that this model can’t mimic, such as persistent location tracking or background music streaming, but for the vast majority of cases users simply want the ability to either get back to where they were (resume a previous task) or to be notified of updates (via toasts). For these scenarios, we think Windows Phone 7 does a great job.

It should be noted that this was posted on March 26 (before Apple’s iPhone OS 4 announcement) and that it’s still unknown if Microsoft will modify (add a task switcher?) this function before RTM (unlikely IMO). On the other hand we already know that Microsoft is working on enabling third party applications to have access to background services in the future (GPS, Music streaming etc..) but this won’t be available in the initial release.