Jul 27 2010

Windows Phone 7 Multitasking

Category: silverlightAdministrator @ 19:59

 

In .NET Rocks! Show number 578 about the Silverlight Pivot Viewer, a listener writes in about the lack of multitasking in Windows Phone 7.

He points out that he often uses a stopwatch on Android and let it “run in the background” while doing other tasks.

This is a perfect example of something that is entirely possible on WP7 if you try to change your mindset and look at what’s possible instead of focusing on limitations.

To prove my point, I created a (from the users point of view) multitasking stopwatch for WP7:

 

What actually happens here is that I cheat and use the WP7 lifecycle events. When the user navigates away from my app, the Application_Deactivated event is fired and I create a tombstone for my application with the start time (when the user pressed start on the stopwatch).

        private void Application_Deactivated(object sender, DeactivatedEventArgs e)

        {

            if(true==PhoneApplicationService.Current.State.ContainsKey(StartTimeKey))

            {

                PhoneApplicationService.Current.State.Remove(StartTimeKey);

            }

            if(StartTime!=null) PhoneApplicationService.Current.State.Add(StartTimeKey,StartTime);

        }

 

When the user presses the Back button, the application is reactivated and is actually started back up, but because a tombstone exists, the Application_Activated event is fired, and I get my start time back.

        private void Application_Activated(object sender, ActivatedEventArgs e)

        {

            if(PhoneApplicationService.Current.State.ContainsKey(StartTimeKey))

            {

                StartTime = PhoneApplicationService.Current.State[StartTimeKey] as DateTime?;

            }

        }

 

If I get a start time, I will auto-start the timer that controls the display and start counting up from the original start-time.

To the user, this is a multitasking stopwatch!

To the developer, sure it’s a little work to shut down and start back up. But the advantage is that I know I always have the full power of the phone hardware when MY app is running.

 

 

Source code: Wp7StopWatch.zip (78.86 kb)

Tags: , , ,

Comments

1.
pingback windowsphone.mobilitydigest.com says:

Pingback from windowsphone.mobilitydigest.com

Mobility Digest - Windows Phone -   A Smart Developer Multitasks Without Multitasking in WP7

2.
pingback techxplosion.net says:

Pingback from techxplosion.net

You can multi task on WP7! | TechXplosion.net

3.
pingback windowsmobileblog.info says:

Pingback from windowsmobileblog.info

Multitasking in Windows Phone 7: it can be done…kinda | Windows Mobile Based Phones

4.
pingback best-smartphone.info says:

Pingback from best-smartphone.info

Multitasking in Windows Phone 7: it can be done…kinda | Best Smartphone Blogging

5.
Beau Allison Beau Allison Australia says:

Great work! When comparing this to the iPhone's Suspended State and Layered API, what really is different ;)

6.
pingback ghazanfaralich.wordpress.com says:

Pingback from ghazanfaralich.wordpress.com

Multitasking in Windows Phone 7: it can be done…kinda « Ghazanfar Ali's Blog

7.
pingback windows.findtechnews.net says:

Pingback from windows.findtechnews.net

Windows News » Multitasking in Windows Phone 7: it can be done…kinda

8.
pingback phone7.wordpress.com says:

Pingback from phone7.wordpress.com

Developer Resources « Phone7

9.
pingback seattlegirlmakesgood.com says:

Pingback from seattlegirlmakesgood.com

CLR | Windows Phone 7 Multitasking

10.
vinay vinay Singapore says:

Then what about the app which is downloading a file and suddenly tombstoned? will it still download the file in the back ground?

11.
Britax Marathon Car Seat Britax Marathon Car Seat United States says:

I actually use my own stop watch, I had know idea that this can be done with WP7.  I'm downloading he script, I will let you know if it works.  Thanks for posting this!!

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading