Vunvulea radu it camp-ro 2012 - building metro style applications on windows 8 using background tasks
1. Building metro style
applications on Windows 8
using background task
Radu Vunvulea
iQuest
http://vunvulearadu.blogspot.com
Twitter: @RaduVunvulea
@ itcampro # itcamp12 Premium conference on Microsoft technologies
2. Mobile &
ITCamp 2012 sponsors Development
@ itcampro # itcamp12 Premium conference on Microsoft technologies
3. Mobile &
Agenda Development
• Why we need background tasks
• Base concepts
• When to use them
• Triggers
• Conditions
• Notifications
• Demo
• Tips and Tricks
• Q&A
@ itcampro # itcamp12 Premium conference on Microsoft technologies
4. Mobile &
Why we need background tasks Development
• What happens with classic applications?
• Where Windows Services vanish?
• Why do I need background tasks?
@ itcampro # itcamp12 Premium conference on Microsoft technologies
5. Mobile &
Why we need background tasks Development
• What happens with classic applications?
• Where Windows Services vanish?
• Why do I need background tasks?
@ itcampro # itcamp12 Premium conference on Microsoft technologies
6. Mobile &
Windows Metro Applications Development
• Similar to Windows Phone applications
• Only threads from the current application
(foreground) are running
@ itcampro # itcamp12 Premium conference on Microsoft technologies
7. Mobile &
Background tasks 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
@ itcampro # itcamp12 Premium conference on Microsoft technologies
8. Mobile &
Background task types Development
• System
– Playback manager
– Push notification
– Background transfer API
• Upload files
• Download files
• User-defined
– Almost any type of custom action
@ itcampro # itcamp12 Premium conference on Microsoft technologies
9. Mobile &
When to use them 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
@ itcampro # itcamp12 Premium conference on Microsoft technologies
10. Mobile &
Background task triggers Development
ControlChannelTrigger ServicingComplete
InternetAvailable SessionConnected
InternetNotAvailable SessionDisconnected
LockScreenApplicationAdded SessionStart
LockScreenApplicationRemoved SmsReceived
MaintenanceTrigger TimeTrigger
NetworkNotificationChannelReset TimeZoneChange
NetworkStateChange UserAway
OnlineIdConnectedStateChange UserPresent
PushNotificationTrigger
@ itcampro # itcamp12 Premium conference on Microsoft technologies
11. Mobile &
Background task triggers Development
ControlChannelTrigger ServicingComplete
InternetAvailable SessionConnected
InternetNotAvailable SessionDisconnected
LockScreenApplicationAdded SessionStart
LockScreenApplicationRemoved SmsReceived
MaintenanceTrigger TimeTrigger
NetworkNotificationChannelReset TimeZoneChange
NetworkStateChange UserAway
OnlineIdConnectedStateChange UserPresent
PushNotificationTrigger
@ itcampro # itcamp12 Premium conference on Microsoft technologies
12. Mobile &
Background task triggers Development
• Some triggers require the application to be on the lock screen
ControlChannelTrigger ServicingComplete
InternetAvailable SessionConnected
InternetNotAvailable SessionDisconnected
LockScreenApplicationAdded SessionStart
LockScreenApplicationRemoved SmsReceived
MaintenanceTrigger TimeTrigger
NetworkNotificationChannelReset TimeZoneChange
NetworkStateChange UserAway
OnlineIdConnectedStateChange UserPresent
PushNotificationTrigger
• An application can register to be notified when it is added or removed
from lock screen
@ itcampro # itcamp12 Premium conference on Microsoft technologies
13. Mobile &
Background task conditions Development
• 0..n conditions can be added to each
background task
• Only when all conditions are met, the given
task is launched
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
14. Mobile &
Background task infrastructure Development
Application System
4. Launch background task
System
My Application 3. Fire trigger
2. Register class with triggers
Control Channel
Control Channel
1. Register trigger Trigger
Trigger
@ itcampro # itcamp12 Premium conference on Microsoft technologies
15. Mobile &
Where a background task run Development
Application BackgroundTaskHost.exe
Application or
BackgroundTaskHost.exe
@ itcampro # itcamp12 Premium conference on Microsoft technologies
16. Mobile &
Where a background task run Development
TimeTrigger
SystemTrigger
ControlChannelTrigger MaintenanceTrigger
Application BackgroundTaskHost.exe
Application or
BackgroundTaskHost.exe
PushNotificationTrigger
@ itcampro # itcamp12 Premium conference on Microsoft technologies
17. Mobile &
Notifications 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
@ itcampro # itcamp12 Premium conference on Microsoft technologies
18. Mobile &
Basic steps to implement 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
@ itcampro # itcamp12 Premium conference on Microsoft technologies
19. Mobile &
Base classes 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
@ itcampro # itcamp12 Premium conference on Microsoft technologies
20. Mobile &
Live demo Development
@ itcampro # itcamp12 Premium conference on Microsoft technologies
21. Mobile &
CPU resource constrains Development
• Limited CPU time for each application
• Use BackgroundTaskDeferral for async calls
CPU resource quota Refresh period
Lock screen app 2 CPU seconds 15 minutes
Non-lock screen app 1 CPU second 2 hours
• Restrictions are applied on AC power also
• Exceptions:
– Control channel
– Push notifications
@ itcampro # itcamp12 Premium conference on Microsoft technologies
22. Mobile &
Network resource constraints Development
• Different network interfaces require different
amount of energy
• Wi-Fi resource constraints:
Average Data throughput, in megabytes (MB) Data throughput, in MB, for non-lock
throughput for lock screen apps screen apps
Every 15” Per day Every 2 hours Per day
1 Mbps 0.1875 18 0.25 3
10 Mbps 1.875 180 2.5 30
20 Mbps 3.75 360 5 60
• On AC power there is no limitation for Wi-Fi
network
@ itcampro # itcamp12 Premium conference on Microsoft technologies
23. Mobile &
Tips and tricks 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
@ itcampro # itcamp12 Premium conference on Microsoft technologies
24. Mobile &
Best practices 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
@ itcampro # itcamp12 Premium conference on Microsoft technologies
25. @ itcampro # itcamp12 Premium conference on Microsoft technologies
26. THE END
Radu Vunvulea
iQuest
http://vunvulearadu.blogspot.com
Twitter: @RaduVunvulea
@ itcampro # itcamp12 Premium conference on Microsoft technologies