SlideShare a Scribd company logo
1 of 31
Background Tasks

Allow apps to execute code when not active
 App is suspended, but background tasks execute
 No UI permitted except for tiles, toasts, and badges
Background tasks implement IBackgroundTask
 Execute in response to triggers (e.g., SystemTrigger)
 Tasks can optionally have conditions attached
BackgroundTaskBuilder class provides API for registering background
tasks
Background Task Constraints

CPU utilization is limited
 Applies to both AC and DC power
 Lock-screen apps get 2 seconds every 15 minutes
 Others receive 1 second every 2 hours
Network utilization is limited
 Limited on DC power; no limit for AC
 Bandwidth varies depending on network hardware
    For example, WiFi gets more bandwidth than mobile
If quota is reached, task is temporarily suspended
Implementing a Background Task

Add "Windows Runtime Component" project to solution and reference it
from main project
Add class that implements IBackgroundTask
 Must be public and sealed
 Override Run; use deferral if performing async calls
Register background task from main app
 First make sure it isn't already registered
 Specify trigger and (optionally) conditions
Add background-task declaration to manifest
Implementing IBackgroundTask
namespace SampleBackgroundTask
{
    public sealed class TimeZoneTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // TODO: Add code that executes in the background
        }
    }
}
Enumerating Background Tasks
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
    var name = task.Value.Name;
}
Registering a Background Task
var builder = new BackgroundTaskBuilder();
builder.Name = "TimeZoneTask";
builder.TaskEntryPoint = "SampleBackgroundTask.TimeZoneTask";

builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));

builder.Register();



                                                    Recurring rather than one-shot
Declaring a Background Task
Unregistering a Background Task
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
    if (task.Value.Name == "TimeZoneTask")
        task.Value.Unregister(false);
}




                         Do not cancel instances of this
                         task that are currently executing
Triggers

Background tasks run when triggers fire
 And when conditions are satisfied (more later)
A background task must have exactly one trigger
 Specified with BackgroundTaskBuilder.SetTrigger
Some triggers can be used by any app
 SystemTrigger and MaintenanceTrigger
Others can only be used by lock-screen apps
 TimerTrigger, PushNotificationTrigger, and ControlChannelTrigger
Trigger Types

                   Req’s Lock
     Trigger                      Host                      Description
                    Screen?
System*             Sometimes      BTH       See System Trigger Types
                                             At most every 15 minutes on
Maintenance*           No          BTH
                                             15 minute boundary; requires AC
Timer*                 Yes         BTH       Like Maintenance; can be on battery
PushNotification       Yes       BTH / App   When WNS pushes raw payload
ControlChannel         Yes         App       For RTC; not for JS



*Can be 1-shot or repeating
BTH == BackgroundTaskHost.exe process (default if not in manifest)
App ==App’s process
System Trigger Types

Types that don't require a lock screen

                Type                                  Description
 InternetAvailable              Internet becomes available
 LockScreenApplicationAdded     Your app is added to the lock screen
 LockScreenApplicationRemoved   Your app is removed from the lock screen
 NetworkStateChange             Network change occurs (e.g., cost change)
 OnlineIdConnectedStateChange   Online ID associated with the account changes
 ServicingComplete              Your app is updated
 SmsReceived                    Device receives an SMS (text) message
 TimeZoneChange                 Device's time zone changes
System Trigger Types, Cont.

Types that require a lock screen

               Type                                Description
                            Fires when a control channel representing a real-time
 ControlChannelReset        network connection is reset and therefore might need to
                            be reestablished
 SessionConnected           Fires when the user logs in
                            Fires when a period of inactivity indicates user is "away"
 UserAway
                            (e.g., when the screen blanks)
                            Fired when activity resumes, indicating user is present (e.g.,
 UserPresent
                            when the mouse is wiggled)
Registering an App-Update Task
var builder = new BackgroundTaskBuilder();
builder.Name = "UpdateTask";
builder.TaskEntryPoint = "SampleBackgroundTask.AppUpdateTask";

builder.SetTrigger(new SystemTrigger(SystemTriggerType.ServicingComplete));

builder.Register();
Registering a Periodic Task
// Register PeriodicTask to execute every 15 minutes
var builder = new BackgroundTaskBuilder();
builder.Name = "PeriodicTask";
builder.TaskEntryPoint = "SampleBackgroundTask.PeriodicTask";
builder.SetTrigger(new MaintenanceTrigger(15, false));
builder.Register();




                 Minimum 15 minutes           Recurring rather than one-shot
Conditions

Background tasks can have conditions attached
 Task runs when trigger fires and all conditions are met
 Added with BackgroundTaskBuilder.AddCondition
Windows.ApplicationModel.Background.System-Condition class models
system conditions
 Whether Internet is available
 Whether user is logged in
 Whether user is present
Condition Types

           Type                                    Description
 InternetAvailable      Runs background task if Internet is available
 InternetNotAvailable   Runs background task if Internet is not available
 SessionConnected       Runs background task if user is logged in
 SessionDisconnected    Runs background task if user is not logged in
 UserNotPresent         Runs background task if user is not present
 UserPresent            Runs background task if user is present
Adding a Condition
var builder = new BackgroundTaskBuilder();
builder.Name = "TimeZoneTask";
builder.TaskEntryPoint = "SampleBackgroundTask.TimeZoneTask";

builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));

builder.Register();
Passing Data from a Task

Task writes data to ApplicationData
  LocalSettings, LocalFolder, etc.
App retrieves data from ApplicationData

// Write result to LocalSettings in the background task
ApplicationData.Current.LocalSettings.Values["Result"] = result;

// Retrieve result from LocalSettings in the app (assumes result is a string)
var result = (string)ApplicationData.Current.LocalSettings.Values["Result"];
Lock-Screen Apps

Apps that are added to the lock screen by users
 Lock screen can display up to 7 apps
 Apps display useful, at-a-glance, real-time information
Must implement background task using one of the following triggers,
which are only available to lock-screen apps
 ControlChannelTrigger (always-connected RTC apps)
 PushNotificationTrigger (receipt of raw notifications)
 TimeTrigger (time-based, like MaintenanceTrigger)
Afforded higher QOS than normal apps
What Can Lock-Screen Apps Do?
Making an App Lock-Screen Capable

Use Application UI tab in manifest editor to specify what the app is
capable of
 Toast notifications, badge updates, tile-text updates
 Tile-text updates require wide logo




                                                 App can display toast notifications


                                                App can update badge and tile text
Lock-Screen Access

Only users can add apps to the lock screen
BackgroundExecutionManager provides API for checking for and
requesting lock-screen access

           Type                                 Description
  GetAccessStatus      Indicates whether app has access to lock screen
                       Prompts the user for access. Method is awaitable and returns
  RequestAccessAsync   BackgroundAccessStatus value. Prompts user one time;
                       subsequent invocations return user's previous decision.
  RemoveAccess         Removes an app from the lock screen
Checking for Lock-Screen Access
var result = BackgroundExecutionManager.GetAccessStatus();

switch (result)
{
    case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
        break;
    case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
        break;
    case BackgroundAccessStatus.Denied:
        break;
    case BackgroundAccessStatus.Unspecified:
        break;
}
Requesting Lock-Screen Access
var result = await BackgroundExecutionManager.RequestAccessAsync();

switch (result)
{
    case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
        break;
    case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
        break;
    case BackgroundAccessStatus.Denied:
        break;
    case BackgroundAccessStatus.Unspecified:
        break;
}
Using a TimeTrigger
var status = BackgroundExecutionManager.GetAccessStatus();

if (status == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
    status == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
{
    var builder = new BackgroundTaskBuilder();
    builder.Name = "TimerTask";
    builder.TaskEntryPoint = "SampleBackgroundTask.TimerTask";
    builder.SetTrigger(new TimeTrigger(15, false));
    builder.Register();
}
Updating a Lock-Screen Badge
// Display a numeric badge
var xml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);
((XmlElement)xml.GetElementsByTagName("badge")[0]).SetAttribute("value", "7");
var bu = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
bu.Update(new BadgeNotification(xml));

// Display a glyph badge
var xml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeGlyph);
((XmlElement)xml.GetElementsByTagName("badge")[0]).SetAttribute("value", "newMessage");
var bu = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
bu.Update(new BadgeNotification(xml));
Updating Lock-Screen Text
var xml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText05);
var text = xml.GetElementsByTagName("text");
text[0].InnerText = "Make awesome apps";
text[1].InnerText = "BUILD Conference";
text[2].InnerText = "9:00 AM - 11:00 AM";
var tu = TileUpdateManager.CreateTileUpdaterForApplication();
tu.Update(new TileNotification(xml));
Background Tasks in Windows Apps

More Related Content

Similar to Background Tasks in Windows Apps

[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade ServerlessKatyShimizu
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade ServerlessKatyShimizu
 
ASP.NET AJAX Basics
ASP.NET AJAX BasicsASP.NET AJAX Basics
ASP.NET AJAX Basicspetrov
 
Taming event-driven software via formal verification
Taming event-driven software via formal verificationTaming event-driven software via formal verification
Taming event-driven software via formal verificationAdaCore
 
Architecting ActionScript 3 applications using PureMVC
Architecting ActionScript 3 applications using PureMVCArchitecting ActionScript 3 applications using PureMVC
Architecting ActionScript 3 applications using PureMVCmarcocasario
 
Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonfNataliya Patsovska
 
Фоновые задачи и многозадачность для Windows Phone
Фоновые задачи и многозадачность для Windows PhoneФоновые задачи и многозадачность для Windows Phone
Фоновые задачи и многозадачность для Windows PhoneMykhail Galushko
 
Reactive & Realtime Web Applications with TurboGears2
Reactive & Realtime Web Applications with TurboGears2Reactive & Realtime Web Applications with TurboGears2
Reactive & Realtime Web Applications with TurboGears2Alessandro Molina
 
Getting your app ready for android n
Getting your app ready for android nGetting your app ready for android n
Getting your app ready for android nSercan Yusuf
 
DataFX 8 (JavaOne 2014)
DataFX 8 (JavaOne 2014)DataFX 8 (JavaOne 2014)
DataFX 8 (JavaOne 2014)Hendrik Ebbers
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web ToolkitsYiguang Hu
 
11 background tasks and multitasking
11   background tasks and multitasking11   background tasks and multitasking
11 background tasks and multitaskingWindowsPhoneRocks
 
Windows services 101 (2004)
Windows services 101 (2004)Windows services 101 (2004)
Windows services 101 (2004)Vatroslav Mihalj
 
What's new in android jakarta gdg (2015-08-26)
What's new in android   jakarta gdg (2015-08-26)What's new in android   jakarta gdg (2015-08-26)
What's new in android jakarta gdg (2015-08-26)Google
 
MongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
MongoDB.local DC 2018: Scaling Realtime Apps with Change StreamsMongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
MongoDB.local DC 2018: Scaling Realtime Apps with Change StreamsMongoDB
 
Thomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalThomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalDroidcon Berlin
 
Performance eng prakash.sahu
Performance eng prakash.sahuPerformance eng prakash.sahu
Performance eng prakash.sahuDr. Prakash Sahu
 
I really need help on this question.Create a program that allows t.pdf
I really need help on this question.Create a program that allows t.pdfI really need help on this question.Create a program that allows t.pdf
I really need help on this question.Create a program that allows t.pdfamitbagga0808
 

Similar to Background Tasks in Windows Apps (20)

PLM and Background Tasks by Jason Fox
PLM and Background Tasks by Jason FoxPLM and Background Tasks by Jason Fox
PLM and Background Tasks by Jason Fox
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless
 
ASP.NET AJAX Basics
ASP.NET AJAX BasicsASP.NET AJAX Basics
ASP.NET AJAX Basics
 
Taming event-driven software via formal verification
Taming event-driven software via formal verificationTaming event-driven software via formal verification
Taming event-driven software via formal verification
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
Architecting ActionScript 3 applications using PureMVC
Architecting ActionScript 3 applications using PureMVCArchitecting ActionScript 3 applications using PureMVC
Architecting ActionScript 3 applications using PureMVC
 
Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonf
 
Фоновые задачи и многозадачность для Windows Phone
Фоновые задачи и многозадачность для Windows PhoneФоновые задачи и многозадачность для Windows Phone
Фоновые задачи и многозадачность для Windows Phone
 
Reactive & Realtime Web Applications with TurboGears2
Reactive & Realtime Web Applications with TurboGears2Reactive & Realtime Web Applications with TurboGears2
Reactive & Realtime Web Applications with TurboGears2
 
Getting your app ready for android n
Getting your app ready for android nGetting your app ready for android n
Getting your app ready for android n
 
DataFX 8 (JavaOne 2014)
DataFX 8 (JavaOne 2014)DataFX 8 (JavaOne 2014)
DataFX 8 (JavaOne 2014)
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web Toolkits
 
11 background tasks and multitasking
11   background tasks and multitasking11   background tasks and multitasking
11 background tasks and multitasking
 
Windows services 101 (2004)
Windows services 101 (2004)Windows services 101 (2004)
Windows services 101 (2004)
 
What's new in android jakarta gdg (2015-08-26)
What's new in android   jakarta gdg (2015-08-26)What's new in android   jakarta gdg (2015-08-26)
What's new in android jakarta gdg (2015-08-26)
 
MongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
MongoDB.local DC 2018: Scaling Realtime Apps with Change StreamsMongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
MongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
 
Thomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalThomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-final
 
Performance eng prakash.sahu
Performance eng prakash.sahuPerformance eng prakash.sahu
Performance eng prakash.sahu
 
I really need help on this question.Create a program that allows t.pdf
I really need help on this question.Create a program that allows t.pdfI really need help on this question.Create a program that allows t.pdf
I really need help on this question.Create a program that allows t.pdf
 

More from Microsoft Developer Network (MSDN) - Belgium and Luxembourg

More from Microsoft Developer Network (MSDN) - Belgium and Luxembourg (20)

Code in the Cloud - Ghent - 20 February 2015
Code in the Cloud - Ghent - 20 February 2015Code in the Cloud - Ghent - 20 February 2015
Code in the Cloud - Ghent - 20 February 2015
 
Executive Summit for ISV & Application builders - January 2015
Executive Summit for ISV & Application builders - January 2015Executive Summit for ISV & Application builders - January 2015
Executive Summit for ISV & Application builders - January 2015
 
Executive Summit for ISV & Application builders - Internet of Things
Executive Summit for ISV & Application builders - Internet of ThingsExecutive Summit for ISV & Application builders - Internet of Things
Executive Summit for ISV & Application builders - Internet of Things
 
Executive Summit for ISV & Application builders - January 2015
Executive Summit for ISV & Application builders - January 2015Executive Summit for ISV & Application builders - January 2015
Executive Summit for ISV & Application builders - January 2015
 
Code in the Cloud - December 8th 2014
Code in the Cloud - December 8th 2014Code in the Cloud - December 8th 2014
Code in the Cloud - December 8th 2014
 
Adam azure presentation
Adam   azure presentationAdam   azure presentation
Adam azure presentation
 
release management
release managementrelease management
release management
 
cloud value for application development
cloud value for application developmentcloud value for application development
cloud value for application development
 
Modern lifecycle management practices
Modern lifecycle management practicesModern lifecycle management practices
Modern lifecycle management practices
 
Belgian visual studio launch 2013
Belgian visual studio launch 2013Belgian visual studio launch 2013
Belgian visual studio launch 2013
 
Windows Azure Virtually Speaking
Windows Azure Virtually SpeakingWindows Azure Virtually Speaking
Windows Azure Virtually Speaking
 
Inside the Microsoft TechDays Belgium Apps
Inside the Microsoft TechDays Belgium AppsInside the Microsoft TechDays Belgium Apps
Inside the Microsoft TechDays Belgium Apps
 
TechDays 2013 Developer Keynote
TechDays 2013 Developer KeynoteTechDays 2013 Developer Keynote
TechDays 2013 Developer Keynote
 
Windows Phone 8 Security Deep Dive
Windows Phone 8 Security Deep DiveWindows Phone 8 Security Deep Dive
Windows Phone 8 Security Deep Dive
 
Deep Dive into Entity Framework 6.0
Deep Dive into Entity Framework 6.0Deep Dive into Entity Framework 6.0
Deep Dive into Entity Framework 6.0
 
Applied MVVM in Windows 8 apps: not your typical MVVM session!
Applied MVVM in Windows 8 apps: not your typical MVVM session!Applied MVVM in Windows 8 apps: not your typical MVVM session!
Applied MVVM in Windows 8 apps: not your typical MVVM session!
 
Building SPA’s (Single Page App) with Backbone.js
Building SPA’s (Single Page App) with Backbone.jsBuilding SPA’s (Single Page App) with Backbone.js
Building SPA’s (Single Page App) with Backbone.js
 
Deep Dive and Best Practices for Windows Azure Storage Services
Deep Dive and Best Practices for Windows Azure Storage ServicesDeep Dive and Best Practices for Windows Azure Storage Services
Deep Dive and Best Practices for Windows Azure Storage Services
 
Building data centric applications for web, desktop and mobile with Entity Fr...
Building data centric applications for web, desktop and mobile with Entity Fr...Building data centric applications for web, desktop and mobile with Entity Fr...
Building data centric applications for web, desktop and mobile with Entity Fr...
 
Bart De Smet Unplugged
Bart De Smet UnpluggedBart De Smet Unplugged
Bart De Smet Unplugged
 

Background Tasks in Windows Apps

  • 1.
  • 2. Background Tasks Allow apps to execute code when not active  App is suspended, but background tasks execute  No UI permitted except for tiles, toasts, and badges Background tasks implement IBackgroundTask  Execute in response to triggers (e.g., SystemTrigger)  Tasks can optionally have conditions attached BackgroundTaskBuilder class provides API for registering background tasks
  • 3. Background Task Constraints CPU utilization is limited  Applies to both AC and DC power  Lock-screen apps get 2 seconds every 15 minutes  Others receive 1 second every 2 hours Network utilization is limited  Limited on DC power; no limit for AC  Bandwidth varies depending on network hardware  For example, WiFi gets more bandwidth than mobile If quota is reached, task is temporarily suspended
  • 4. Implementing a Background Task Add "Windows Runtime Component" project to solution and reference it from main project Add class that implements IBackgroundTask  Must be public and sealed  Override Run; use deferral if performing async calls Register background task from main app  First make sure it isn't already registered  Specify trigger and (optionally) conditions Add background-task declaration to manifest
  • 5. Implementing IBackgroundTask namespace SampleBackgroundTask { public sealed class TimeZoneTask : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { // TODO: Add code that executes in the background } } }
  • 6. Enumerating Background Tasks foreach (var task in BackgroundTaskRegistration.AllTasks) { var name = task.Value.Name; }
  • 7. Registering a Background Task var builder = new BackgroundTaskBuilder(); builder.Name = "TimeZoneTask"; builder.TaskEntryPoint = "SampleBackgroundTask.TimeZoneTask"; builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false)); builder.Register(); Recurring rather than one-shot
  • 9. Unregistering a Background Task foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == "TimeZoneTask") task.Value.Unregister(false); } Do not cancel instances of this task that are currently executing
  • 10.
  • 11. Triggers Background tasks run when triggers fire  And when conditions are satisfied (more later) A background task must have exactly one trigger  Specified with BackgroundTaskBuilder.SetTrigger Some triggers can be used by any app  SystemTrigger and MaintenanceTrigger Others can only be used by lock-screen apps  TimerTrigger, PushNotificationTrigger, and ControlChannelTrigger
  • 12. Trigger Types Req’s Lock Trigger Host Description Screen? System* Sometimes BTH See System Trigger Types At most every 15 minutes on Maintenance* No BTH 15 minute boundary; requires AC Timer* Yes BTH Like Maintenance; can be on battery PushNotification Yes BTH / App When WNS pushes raw payload ControlChannel Yes App For RTC; not for JS *Can be 1-shot or repeating BTH == BackgroundTaskHost.exe process (default if not in manifest) App ==App’s process
  • 13. System Trigger Types Types that don't require a lock screen Type Description InternetAvailable Internet becomes available LockScreenApplicationAdded Your app is added to the lock screen LockScreenApplicationRemoved Your app is removed from the lock screen NetworkStateChange Network change occurs (e.g., cost change) OnlineIdConnectedStateChange Online ID associated with the account changes ServicingComplete Your app is updated SmsReceived Device receives an SMS (text) message TimeZoneChange Device's time zone changes
  • 14. System Trigger Types, Cont. Types that require a lock screen Type Description Fires when a control channel representing a real-time ControlChannelReset network connection is reset and therefore might need to be reestablished SessionConnected Fires when the user logs in Fires when a period of inactivity indicates user is "away" UserAway (e.g., when the screen blanks) Fired when activity resumes, indicating user is present (e.g., UserPresent when the mouse is wiggled)
  • 15. Registering an App-Update Task var builder = new BackgroundTaskBuilder(); builder.Name = "UpdateTask"; builder.TaskEntryPoint = "SampleBackgroundTask.AppUpdateTask"; builder.SetTrigger(new SystemTrigger(SystemTriggerType.ServicingComplete)); builder.Register();
  • 16. Registering a Periodic Task // Register PeriodicTask to execute every 15 minutes var builder = new BackgroundTaskBuilder(); builder.Name = "PeriodicTask"; builder.TaskEntryPoint = "SampleBackgroundTask.PeriodicTask"; builder.SetTrigger(new MaintenanceTrigger(15, false)); builder.Register(); Minimum 15 minutes Recurring rather than one-shot
  • 17. Conditions Background tasks can have conditions attached  Task runs when trigger fires and all conditions are met  Added with BackgroundTaskBuilder.AddCondition Windows.ApplicationModel.Background.System-Condition class models system conditions  Whether Internet is available  Whether user is logged in  Whether user is present
  • 18. Condition Types Type Description InternetAvailable Runs background task if Internet is available InternetNotAvailable Runs background task if Internet is not available SessionConnected Runs background task if user is logged in SessionDisconnected Runs background task if user is not logged in UserNotPresent Runs background task if user is not present UserPresent Runs background task if user is present
  • 19. Adding a Condition var builder = new BackgroundTaskBuilder(); builder.Name = "TimeZoneTask"; builder.TaskEntryPoint = "SampleBackgroundTask.TimeZoneTask"; builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false)); builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable)); builder.Register();
  • 20. Passing Data from a Task Task writes data to ApplicationData LocalSettings, LocalFolder, etc. App retrieves data from ApplicationData // Write result to LocalSettings in the background task ApplicationData.Current.LocalSettings.Values["Result"] = result; // Retrieve result from LocalSettings in the app (assumes result is a string) var result = (string)ApplicationData.Current.LocalSettings.Values["Result"];
  • 21.
  • 22. Lock-Screen Apps Apps that are added to the lock screen by users  Lock screen can display up to 7 apps  Apps display useful, at-a-glance, real-time information Must implement background task using one of the following triggers, which are only available to lock-screen apps  ControlChannelTrigger (always-connected RTC apps)  PushNotificationTrigger (receipt of raw notifications)  TimeTrigger (time-based, like MaintenanceTrigger) Afforded higher QOS than normal apps
  • 24. Making an App Lock-Screen Capable Use Application UI tab in manifest editor to specify what the app is capable of  Toast notifications, badge updates, tile-text updates  Tile-text updates require wide logo App can display toast notifications App can update badge and tile text
  • 25. Lock-Screen Access Only users can add apps to the lock screen BackgroundExecutionManager provides API for checking for and requesting lock-screen access Type Description GetAccessStatus Indicates whether app has access to lock screen Prompts the user for access. Method is awaitable and returns RequestAccessAsync BackgroundAccessStatus value. Prompts user one time; subsequent invocations return user's previous decision. RemoveAccess Removes an app from the lock screen
  • 26. Checking for Lock-Screen Access var result = BackgroundExecutionManager.GetAccessStatus(); switch (result) { case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity: break; case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity: break; case BackgroundAccessStatus.Denied: break; case BackgroundAccessStatus.Unspecified: break; }
  • 27. Requesting Lock-Screen Access var result = await BackgroundExecutionManager.RequestAccessAsync(); switch (result) { case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity: break; case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity: break; case BackgroundAccessStatus.Denied: break; case BackgroundAccessStatus.Unspecified: break; }
  • 28. Using a TimeTrigger var status = BackgroundExecutionManager.GetAccessStatus(); if (status == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || status == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity) { var builder = new BackgroundTaskBuilder(); builder.Name = "TimerTask"; builder.TaskEntryPoint = "SampleBackgroundTask.TimerTask"; builder.SetTrigger(new TimeTrigger(15, false)); builder.Register(); }
  • 29. Updating a Lock-Screen Badge // Display a numeric badge var xml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber); ((XmlElement)xml.GetElementsByTagName("badge")[0]).SetAttribute("value", "7"); var bu = BadgeUpdateManager.CreateBadgeUpdaterForApplication(); bu.Update(new BadgeNotification(xml)); // Display a glyph badge var xml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeGlyph); ((XmlElement)xml.GetElementsByTagName("badge")[0]).SetAttribute("value", "newMessage"); var bu = BadgeUpdateManager.CreateBadgeUpdaterForApplication(); bu.Update(new BadgeNotification(xml));
  • 30. Updating Lock-Screen Text var xml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText05); var text = xml.GetElementsByTagName("text"); text[0].InnerText = "Make awesome apps"; text[1].InnerText = "BUILD Conference"; text[2].InnerText = "9:00 AM - 11:00 AM"; var tu = TileUpdateManager.CreateTileUpdaterForApplication(); tu.Update(new TileNotification(xml));