software solutions for a mobile world

WP7: Writing a Well-Behaved Threaded Application

The latest CTP release of Windows Phone 7 introduces a new managed library which exposes events you can monitor to catch changes in state in your application. The assembly is Microsoft.Phone.Execution.dll. This contains a WindowsPhoneEvents class which exposes two static methods – Pause and Resume. You can handle these events to detect when your application is dismissed and reactivated. This allows you to stop unnecessary processing and be a good citizen. I put together a simple example which shows how to pause a background thread based on these events and you can see the results by running it on the Emulator and switching to the home screen (Windows button) and returning to your application (Back button).

The sample uses a ManualResetEvent to block the background thread to avoid it consuming processor cycles while the app is paused.

WindowsPhoneThreading.zip (22kb)

 

Also in this assembly is the StreamPersister class. Unfortunately there is no documentation for this at the moment but an instance is passed in the Pause and Resume eventargs which will presumably allow you to safely persist state data which you can then load when your app is resumed. Hopefully there will be some official documentation on this to complete the picture.


Posted May 01 2010, 08:30 PM by Peter Foot
Filed under:

Comments

Twitter Trackbacks for WP7: Writing a Well-Behaved Threaded Application [appamundi.com] on Topsy.com wrote Twitter Trackbacks for WP7: Writing a Well-Behaved Threaded Application [appamundi.com] on Topsy.com
on 05-01-2010 22:47

Pingback from  Twitter Trackbacks for                 WP7: Writing a Well-Behaved Threaded Application         [appamundi.com]        on Topsy.com

Microsoft Weblogs wrote Windows Client Developer Roundup for 5/3/2010
on 05-03-2010 0:42

This is Windows Client Developer roundup #22. The Windows Client Developer Roundup aggregates information

Community Blogs wrote Windows Client Developer Roundup for 5/3/2010
on 05-03-2010 12:36

This is Windows Client Developer roundup #22. The Windows Client Developer Roundup aggregates information

Alberto Silva wrote Windows Phone 7 – Experiência para programadores (XVI)
on 05-03-2010 19:36

Upgrading Windows Phone 7 Projects to April CTP community.softteq.com/.../2010

Copyright © 2011 APPA Mundi Limited. All Rights Reserved. Terms of Use and Privacy Policy. OrcsWeb's Windows Cloud Server Hosting