Rob Irving is a senior software engineer and Microsoft MVP who specializes in Windows platform development. In this presentation, he discusses new background capabilities in Windows Phone 8.1 including triggered background tasks, geofencing, and rendering tiles from background tasks. He explains how background tasks now have quotas based on CPU usage instead of time limits and how requesting access can provide the full quota.
Unlocking the Future of AI Agents with Large Language Models
Multitasking and Triggered Background Processing in Windows Phone 8.1
1. Multitasking and Triggered Background
Processing in Windows Phone 8.1
Rob Irving
Senior Software Engineer – ALK Technologies
Microsoft MVP – Windows Platform Development
www.robwirving.com
@robwirving
2. Rob Irving
• Senior Software Engineer at ALK Technologies
• Microsoft MVP – Windows Platform Development
• Led development effort to bring CoPilot GPS
navigation app to Windows Phone 8
• In top 10 Free Navigation Apps in Window Phone store
• Windows Phone Dev by day and night
• Released 7 of my own apps to Windows Phone store
• http://bit.ly/AppsByRWI
• Blog: www.robwirving.com
• Twitter: @robwirving
3. Agenda
• What are Background Tasks
• History on Windows Phone 8
• What’s new in Windows Phone 8.1
• Triggered Background Tasks
• Geofencing
• XAML Tile Rendering
9. Hosted in a separate process
One trigger per task, multiple
conditions
10. Trigger Usage
System Trigger
UserPresent , UserAway,
SessionConnected,
TimeZoneChange,
NetworkStateChange,
InternetAvailable,
ServicingComplete
Run code on system events
TimeTrigger Data synchronization
MaintenanceTrigger Perform maintenance work on AC power
20. Render a Tile from your BackgroundTask!
New class XamlRenderingBackgroundTask
BackgroundTask + XAML Visual Tree
Use RenderTargetBitmap to render XAML visual tree
21.
22.
23.
24.
25.
26.
27. New in Windows Phone 8.1
Sort and manage which apps can
execute in the background
Controls number of apps that can
run in the background
28. Quotas are based on actual CPU usage instead of wall clock
time limits only
Wall-clock CPU quota will also be enforced (at least 30 secs)
Memory quota scales based on device capabilities
TimeTrigger will have a 30 min floor on Phone
Call RequestAccessAsync() to get your full quota!
Anytime app is running code while not in foreground
Great way to enhance user experience, get users to interact with your app
History of Multitasking in Windows Phone 7 & 8
Windows Phone 7 didn’t really have multitasking
Windows Phone 8 added new mechanisms to provide content when not in the foreground app
Live tiles or toasts – triggered by push notifications or scheduled
OS Brokered tasks like background downloads
Background tasks and agents which would run periodically
In WP8 we had PeriodicTasks and ResourceIntensiveTasks which would run periodically every half hour or so.
Could be used to download new content for the user, update tiles, etc.
Background Transfer Service allowed you to download large files while not in the foreground
Background location tracking is a special exception for navigation apps like CoPilot. As long as these apps are still actively using the GPS they would be allowed to continue running while not in the foreground.
VOIP and Audio Agents were another special case of background execution for apps like Skype or Pandora.
The new Windows Runtime Background execution model is centered around Background Tasks.
Rather then running periodically they respond to triggers or conditions.
This is a converged API, so it’s the same background tasks that are available for Windows Apps.
Because it’s a converged API you do need to request access to run in the background.. On Windows this would display a popup, but on Windows Phone there’s no UI. You still need to check though, and there are certain conditions that would cause background access to not be granted, such as if the user disabled your app through the battery saver settings.
Here are some of the different triggers available. If you’ve done any development with Windows Store apps these may be familiar because it is a converged API.
Go over System Triggers
TimeTrigger run task on an interval, must specify atleast a 30 minute intervals, but on windows phone interval will usually be no less then half an hour.
Maintenance Trigger similar to the resource intensive task, run resource intensive code when the phone is plugged in
Demo Time
Demo in WP8.1 Silverlight, all of the new multitasking features for Windows Universal Apps are also available for Windows Phone Silverlight 8.1
Registering background task in C#
Display available memory in C# toast
At Build all the sessions I went to about background tasks told us that we should be writing our background task code in C++. Background tasks have a limited amount of memory resources available to them ~16MB, by not loading up the CLR you can save a few MBs and hopefully avoid Out of Memory exceptions. You can still write your apps in C#, but the nice thing about the Windows Runtime is that you can interoperate between C#, C++ and even WinJS really easily.
Convert C# Background Task into a C++ project
Modify code and manifest to register C++ background task
All of the tasks I’ve showed you so far were already there for Windows Store, so now we’ll go into some new forms of multitasking
All this is available for Silverlight 8.1. So if you’re already invested with apps developed in Windows Phone 8, like I am. Then you can still benefit from all these new background tasks without having to migrate to the Windows Univeral App Model.
When you upgrade to 8.1 your 8.0 style background agents will be hosted on top of the modern background infrastructure and should run better with the newer resource management.
Note that you should make sure you don’t have a mix of 8.0 BG Agents and 8.1 BG Tasks as conflicts will occur with mixing the Silverlight CLR and WinRT CLR. If you write your BG Agent/Task code in C++ though you’ll be fine.
Not a Bluetooth expert so I don’t have a demo to show about any of the new Bluetooth functionality here, but I’m really excited about it.
Bluetooth LE is a new feature for Windows Phone 8.1 it should be available in many of the newer phones, Lumia Icon, 930, 1520 and 630.
Bluetooth triggers are based on existing WinRT Bluetooth APIs
DeviceChange is pretty self explanatory if a Device connects or reconnects to the phone this trigger will be raised
DeviceUpdate trigger is going to be core to how fitness apps like FitBit could integrate with Windows Phone. With these you’ll be able to have a long running background task that communicates with Bluetooth LE devices like heart rate monitors or pedometers
You can now have your app setup a Windows Notification Service channel and associate it with a background task
Then any raw push notifications that come through on that channel will activate the background task.
This can be really useful for news reader applications or social media like twitter or facebook. I know one thing that frustrates me with twitter is that I’ll see a notification come through from twitter, and click on it to activate the app. When I go into the app though it still needs to connect to the service and fetch the new tweets. With these new background tasks that handle RAW notifications a service like twitter could get the notification, connect to the service to refresh content and THEN show a toast notification to let me know that someone mentioned me. It’ll make the app appear more responsive to me, even though in reality I’m getting my toast notification a second or two later.
One of the things done most frequently with periodic tasks in WP8.0 was updating tiles. A lot of developers would take some XAML, render it to a bitmap and then update a tile with that bitmap.
If you’ve done any development on Windows 8 you might know that this wasn’t possible with Windows 8 background tasks. It’s not possible to access the XAML tree there.
So a new type of background task is available for Windows Phone that gives access to the XAML visual tree.
So in this background task you’ll basically create some XAML, use RenderTargetBitmap to render the XAML to a PNG and then you can use that PNG to update your tile
So the interesting thing about this demo is that Microsoft wants you to use C++ for XamlRenderingBackgroundTasks so much that they didn’t make a C# version of the sample code. But when working on this presentation I was curious about what the memory savings would look like in a more complex demo like this, so I converted the C++ task into a C# one to compare them.