Can Windows Phone 7 Multitask?

[UPDATE – April 12, 2010: the test below is a bug in the Windows Phone 7 CTP Emulator as I expected.  Expect the Pause/Resume/Suspend functionality to be fixed in future emulators and to work on a real device.  Don’t rely on your threads running in the background in future versions and offload background processing to a server and use Push Notifications to update/notify device.]

There has been a lot of talk about how Microsoft is behind the ball on Windows Phone 7 since it can’t multitask since Apple announced iPhone 4.0 OS will now include multitasking.

What’s my take on this?  Well, Windows Phone 7 is based on Windows Embedded CE.  Without going into too much details on Windows CE, it is basically a real-time operating system and is not a trimmed down version of their desktop Windows OS.  Windows CE is it’s own full fledged real-time operating system and yes it can do multitasking. 

In all the years I’ve been developing Windows Phone and Windows CE software for customers, I have seen lots of applications that just go out and kill the battery, launch X number of threads or do things that are just not too smart and really kills the end user experience on the device. Think about it, when an app kills the battery in 1hr, the end user will usually say “this phone sucks” and returns it. In reality it’s not the phones fault, it’s the crappy app! This has always been a problem that has plagued Pocket PC/Windows Mobile/Windows Phone 5.x – 6.x for some time.  Windows Phone 7 is about the end user and making sure their experience with the phone is top notch.  This post on Windows Phone 7 and “Focus” by Charlie Kindel talks a bit more about the end user experience.

Back to the question, Can Windows Phone 7 Multitask?  Of course it can, it’s just not fully exposed to third party developers.  Things like the Zune player will continue to run in the background even if it’s not the foreground app.  What the Windows Phone 7 team has done is scale back some of the power a third party developer has so the end user experience is not compromised. 

So as a third party developer what’s available to us?  Peter Torr gave a session at MIX10 called Building Windows Phone Applications with Silverlight (Part II) and shows this diagram of application state when jumping to another application.



So your application has four states:

  1. Running – Here your application is started by the user and will run
  2. Paused – User decided to switch over to another application or answered a phone call (remember the device is a phone too :).  Here you should save your application state in case you go into “Suspended” state
  3. Suspended – Here you could assume your application is being killed by the OS.  That’s ok as long as you saved your state when you are paused.
  4. Resume – The user has started your app again and you want to resume where they left off since you saved state when paused

Because there is a suspended state, it doesn’t mean your app will always get suspended.  Your app will only get suspended if they OS feels that it is running low on resources.  (Remember, the phone doesn’t run a quad core processor with 8GB of ram :)  Once the OS is running low on resources it will start Suspending (or killing) apps most likely starting with the one that has not been in the foreground for the longest time.  To me it sounds very similar to WM_HIBERNATE and old school Windows Phone and Windows CE developers will know all about this.

So, being a curious developer, I tested the “Paused” state above but because the Windows Phone 7 Dev tools are CTP I can’t come to a definite conclusion.  Basically I created a simple app that continues counting on a background thread and updates the UI


Without hitting the “Back” button (because that will close my app), I clicked on the “Start” button and started the EnergizeIT Mobile app and logged in to get to the main screen.  I then clicked on the “Start” button again, opened the Start Menu and opened the WPMultitask1 sample app and my counter was incremented to 1724 from 1676.  In fact, I’ve been running it in the background for about 1,700s while I was writing this post.


Because I am using a CTP, maybe the “Pause” state does not work yet.  Couldn’t really test the “Suspended” state because all the Windows Phone 7 emulator has is Internet Explorer so most likely can’t use up all resources on the device.

In the end, Windows Phone 7 can do multitasking but is just limited to third party developers.  My test above shows a Thread in an app will continue to run while not being the foreground app.  Since iPhone OS 4.0 was just announced and now will support multitasking, my guess is the Windows Phone 7 team may consider opening up multitasking a bit more to third party developers.  Maybe during marketplace submission there will be a check box that says “My application requires access to run in the background” and the testing process will be more rigorous (yes I’m over simplifying it). The OS has the power to do multitasking, it’s just a matter of doing it right and preserving the end user experience.

Here is the code to run the threads:

/// <summary> /// thread to update the counter /// </summary> private Thread m_counterThread; /// <summary> /// Waithandle to tell the thread to stop /// </summary> private ManualResetEvent m_waitHandle = new ManualResetEvent(false); /// <summary> /// The current count to display /// </summary> private int m_count = 0; public MainPage() { InitializeComponent(); SupportedOrientations = SupportedPageOrientation.Portrait |
SupportedPageOrientation.Landscape; //enable disable the buttons btnStop.IsEnabled = false; } private void btnStart_Click(object sender, RoutedEventArgs e) { if (m_counterThread == null) { m_waitHandle.Reset(); WaitHandle[] handles = new List<WaitHandle> {m_waitHandle}.ToArray(); m_counterThread = new Thread(() => { //Main counter thread int index = WaitHandle.WaitTimeout; while (index == WaitHandle.WaitTimeout) { //incrememnt the counter m_count++; //update the counter label this.Dispatcher.BeginInvoke(new Action(() => { //Update the lable counter.Text = m_count.ToString(); })); //Wait for the stop handle index = WaitHandle.WaitAny(handles, 950); } }); //Start the thread m_counterThread.Start(); } //enable disable the buttons btnStart.IsEnabled = false; btnStop.IsEnabled = true; } private void btnStop_Click(object sender, RoutedEventArgs e) { if (m_counterThread != null) { //tell the main thread loop to stop m_waitHandle.Set(); //wait here till the thread stops m_counterThread.Join(); m_counterThread = null; //enable disable the buttons btnStart.IsEnabled = true; btnStop.IsEnabled = false; } }

And here is the partial Xaml for the page (ContentGrid)

<Grid x:Name="ContentGrid" Grid.Row="1">
    <TextBlock x:Name="counter" Text="[Counter Text]" 
               Style="{StaticResource PhoneTextPageTitle2Style}" 
               TextAlignment="Center" />
    <Button x:Name="btnStart" 
            Content="Start Thread" 
            Margin="70,334,76,205" />
    <Button x:Name="btnStop" 
            Content="Stop Thread" 
            Margin="70,453,76,90" />

Warning: count(): Parameter must be an array or an object that implements Countable in /nfs/c07/h02/mnt/110221/domains/ on line 405

One thought on “Can Windows Phone 7 Multitask?”

  1. Good post, Mark.
    But your test with background working is a bug, but no excpected behavior. It will be fixed with future CTP/release, as I know. But your demo is clearly shows that WP7 can work with multitasking.

Leave a Reply