View
699
Download
2
Category
Tags:
Preview:
DESCRIPTION
Slides that describes how background task works on Windows 8. ITCamp 2012, Cluj-Napoca
Citation preview
itcampro@ itcamp12# Premium conference on Microsoft technologies
Building metro style applications on Windows 8
using background task
Radu Vunvulea iQuest
http://vunvulearadu.blogspot.comTwitter: @RaduVunvulea
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & DevelopmentITCamp 2012 sponsors
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• Why we need background tasks• Base concepts• When to use them• Triggers • Conditions• Notifications• Demo• Tips and Tricks• Q&A
Agenda
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• What happens with classic applications?
• Where Windows Services vanish?
• Why do I need background tasks?
Why we need background tasks
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• What happens with classic applications?
• Where Windows Services vanish?
• Why do I need background tasks?
Why we need background tasks
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• Similar to Windows Phone applications• Only threads from the current
application (foreground) are running
Windows Metro Applications
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• Running code in background• The background task doesn’t depend
on application state• Small work items• No interaction with UI• What we gain:– Smooth user experience– Long battery life
Background tasks
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• System– Playback manager– Push notification– Background transfer API• Upload files• Download files
• User-defined– Almost any type of custom action
Background task types
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• Receive chat message• Process an incoming SMS• Download email• Display a toast notification• Execute action when something
changes• Display UI, play music• Process photos• Index data
When to use them
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
ControlChannelTrigger ServicingComplete
InternetAvailable SessionConnected
InternetNotAvailable SessionDisconnected
LockScreenApplicationAdded SessionStart
LockScreenApplicationRemoved SmsReceived
MaintenanceTrigger TimeTrigger
NetworkNotificationChannelReset TimeZoneChange
NetworkStateChange UserAway
OnlineIdConnectedStateChange UserPresent
PushNotificationTrigger
Background task triggers
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
ControlChannelTrigger ServicingComplete
InternetAvailable SessionConnected
InternetNotAvailable SessionDisconnected
LockScreenApplicationAdded SessionStart
LockScreenApplicationRemoved SmsReceived
MaintenanceTrigger TimeTrigger
NetworkNotificationChannelReset TimeZoneChange
NetworkStateChange UserAway
OnlineIdConnectedStateChange UserPresent
PushNotificationTrigger
Background task triggers
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
ControlChannelTrigger ServicingComplete
InternetAvailable SessionConnected
InternetNotAvailable SessionDisconnected
LockScreenApplicationAdded SessionStart
LockScreenApplicationRemoved SmsReceived
MaintenanceTrigger TimeTrigger
NetworkNotificationChannelReset TimeZoneChange
NetworkStateChange UserAway
OnlineIdConnectedStateChange UserPresent
PushNotificationTrigger
Background task triggers• Some triggers require the application to be on the lock screen
• An application can register to be notified when it is added or removed from lock screen
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• 0..n conditions can be added to each background task
• Only when all conditions are met, the given task is launched
Background task conditions
Background task condition
The condition that must be satisfied
InternetAvailable The Internet must be available.InternetNotAvailable The Internet must be unavailable.SessionConnected The session must be connected.SessionDisconnected The session must be
disconnected.UserNotPresent The user must be away.UserPresent The user must be present.
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
Control ChannelTrigger
Background task infrastructure
My Application
System
Control ChannelTrigger
SystemApplication
1. Register trigger
2. Register class with triggers
4. Launch background task
3. Fire trigger
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
Application
Application or BackgroundTaskHos
t.exe
BackgroundTaskHost.exe
Where a background task run
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
Application
Application or BackgroundTaskHos
t.exe
BackgroundTaskHost.exe
Where a background task run
TimeTriggerSystemTrigger
MaintenanceTriggerControlChannelTrigger
PushNotificationTrigger
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• The following notification can send to the base application:– Progress notifications– Completion notifications– Exceptions thrown
• A background task can be receive cancellations notifications only from operating system– The task have 5s to save his state until the
system close the application
Notifications
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• JavaScript– Add a new dedicated worker file
• C#/C++– Add a new Windows 8 Metro style app class library
project to the solution. – Add a reference to the newly added class library
project in the main Windows 8 Metro style app project that registers the background tasks.
– Ensure that the output type of the class library is Winmd and not DLL.
– Add the required manifest entries for background tasks to the Windows 8 Metro style app
– Seal the class that implements the IBackgroundTask
Basic steps to implement
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• IBackgroundTask – used to implement the background task
• IBackgroundTaskInstance – provides access to a background task instance
• BackgroundTaskDeferral – used for async calls from background task
• BackgroundTaskBuilder – used to register a background task
• BackgroundTaskRegistration – a background class that was registered
• BackgroundTaskRegistration.AllTasks – get a list of tasks of the current application
Base classes
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & DevelopmentLive demo
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• Limited CPU time for each application• Use BackgroundTaskDeferral for async
calls
• Restrictions are applied on AC power also
• Exceptions:– Control channel– Push notifications
CPU resource constrains
CPU resource quota Refresh period
Lock screen app 2 CPU seconds 15 minutes
Non-lock screen app 1 CPU second 2 hours
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• Different network interfaces require different amount of energy
• Wi-Fi resource constraints:
• On AC power there is no limitation for Wi-Fi network
Network resource constraints
Average throughput
Data throughput, in megabytes (MB) for lock screen apps
Data throughput, in MB, for non-lock screen apps
Every 15” Per day Every 2 hours Per day1 Mbps 0.1875 18 0.25 3
10 Mbps 1.875 180 2.5 30
20 Mbps 3.75 360 5 60
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• TimeTrigger minimum time interval is 15 minutes
• Progress and Complete handlers needs to be associated each time when application start
• BackgroundTaskDeferral can be used for async calls
• Don’t rely on global pool when all allocated CPU time was used
• When a background task is suspended a message is logged in the Event Viewer.
• Share data between application and background task only by files or State Manager
Tips and tricks
itcampro@ itcamp12# Premium conference on Microsoft technologies
Mobile & Development
• Design background tasks to be short lived• Use persistent storage to share data
between the background task and the app
• Verify if the app needs to be on the lock screen
• Do not display UI other than toast, tiles or badges from a background task.
• Do not rely on user interaction in background tasks
Best practices
itcampro@ itcamp12# Premium conference on Microsoft technologies
itcampro@ itcamp12# Premium conference on Microsoft technologies
THE END
Radu Vunvulea iQuest
http://vunvulearadu.blogspot.comTwitter: @RaduVunvulea
Recommended