vunvulea radu it camp-ro 2012 - building metro style applications on windows 8 using background...

26
itcampro @ itcamp12 # Premium conference on Microsoft technologies Building metro style applications on Windows 8 using background task Radu Vunvulea iQuest http://vunvulearadu.blogspot.com Twitter: @RaduVunvulea

Upload: radu-vunvulea

Post on 13-May-2015

699 views

Category:

Technology


2 download

DESCRIPTION

Slides that describes how background task works on Windows 8. ITCamp 2012, Cluj-Napoca

TRANSCRIPT

Page 1: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 2: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

itcampro@ itcamp12# Premium conference on Microsoft technologies

Mobile & DevelopmentITCamp 2012 sponsors

Page 3: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 4: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using 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

Page 5: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using 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

Page 6: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using 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

Page 7: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 8: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using 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

Page 9: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 10: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 11: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 12: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 13: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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.

Page 14: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 15: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

itcampro@ itcamp12# Premium conference on Microsoft technologies

Mobile & Development

Application

Application or BackgroundTaskHos

t.exe

BackgroundTaskHost.exe

Where a background task run

Page 16: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 17: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 18: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 19: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 20: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

itcampro@ itcamp12# Premium conference on Microsoft technologies

Mobile & DevelopmentLive demo

Page 21: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 22: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 23: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 24: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

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

Page 25: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

itcampro@ itcamp12# Premium conference on Microsoft technologies

Page 26: Vunvulea radu   it camp-ro 2012 - building metro style applications on windows 8 using background tasks

itcampro@ itcamp12# Premium conference on Microsoft technologies

THE END

Radu Vunvulea iQuest

http://vunvulearadu.blogspot.comTwitter: @RaduVunvulea