BackgroundTaskBuilder BackgroundTaskBuilder BackgroundTaskBuilder BackgroundTaskBuilder Class

Definition

Represents a background task to register with the system.

public : sealed class BackgroundTaskBuilder : IBackgroundTaskBuilder, IBackgroundTaskBuilder2, IBackgroundTaskBuilder3, IBackgroundTaskBuilder4public sealed class BackgroundTaskBuilder : IBackgroundTaskBuilder, IBackgroundTaskBuilder2, IBackgroundTaskBuilder3, IBackgroundTaskBuilder4Public NotInheritable Class BackgroundTaskBuilder Implements IBackgroundTaskBuilder, IBackgroundTaskBuilder2, IBackgroundTaskBuilder3, IBackgroundTaskBuilder4// You can use this class in JavaScript.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

Note

This class is not agile, which means that you need to consider its threading model and marshaling behavior. For more info, see Threading and Marshaling (C++/CX) and Using Windows Runtime objects in a multithreaded environment (.NET).

If your background task requires network connectivity, be aware of the following:

** Network related triggers**

  • Use a SocketActivityTrigger to activate the background task when a packet is received and you need to perform a short-lived task. After performing the task, the background task should terminate to save power.
  • Use a ControlChannelTrigger to activate the background task when a packet is received and you need to perform a long-lived task.

** Network related conditions and flags**

  • Add the InternetAvailable condition (BackgroundTaskBuilder.AddCondition ) to your background task to delay triggering the background task until the network stack is running. This condition saves power because the background task won't execute until network access is available. This condition does not provide real-time activation.

Regardless of the trigger you use, set IsNetworkRequested on your background task to ensure that the network stays up while the background task runs. This tells the background task infrastructure to keep the network up while the task is executing, even if the device has entered Connected Standby mode. If your background task does not use IsNetworkRequested, then your background task will not be able to access the network when in Connected Standby mode (for example, when a phone's screen is turned off.)

Constructors

BackgroundTaskBuilder() BackgroundTaskBuilder() BackgroundTaskBuilder() BackgroundTaskBuilder()

Creates an instance of the BackgroundTaskBuilder class.

public : BackgroundTaskBuilder()public BackgroundTaskBuilder()Public Sub New()// You can use this method in JavaScript.

Properties

CancelOnConditionLoss CancelOnConditionLoss CancelOnConditionLoss CancelOnConditionLoss

Indicates whether the background task will be canceled if at least one of its required conditions is no longer met.

public : PlatForm::Boolean CancelOnConditionLoss { get; set; }public bool CancelOnConditionLoss { get; set; }Public ReadWrite Property CancelOnConditionLoss As bool// You can use this property in JavaScript.
Value
PlatForm::Boolean bool bool bool

Whether or not the background task will be canceled if at least one of its required conditions is no longer met.

IsNetworkRequested IsNetworkRequested IsNetworkRequested IsNetworkRequested

Indicates whether the background task needs network connectivity in order to run.

Set IsNetworkRequested to true to ensure that the network stays up while the background task runs. This tells the background task infrastructure to keep the network up while the task is executing, even if the device has entered Connected Standby mode (for example, when a phone's screen is turned off).

public : PlatForm::Boolean IsNetworkRequested { get; set; }public bool IsNetworkRequested { get; set; }Public ReadWrite Property IsNetworkRequested As bool// You can use this property in JavaScript.
Value
PlatForm::Boolean bool bool bool

A value indicating whether the background task requests network connectivity.

Name Name Name Name

Gets or sets the name of a background task.

public : PlatForm::String Name { get; set; }public string Name { get; set; }Public ReadWrite Property Name As string// You can use this property in JavaScript.
Value
PlatForm::String string string string

A description of the background task.

TaskEntryPoint TaskEntryPoint TaskEntryPoint TaskEntryPoint

Gets or sets the class that performs the work of a background task.

public : PlatForm::String TaskEntryPoint { get; set; }public string TaskEntryPoint { get; set; }Public ReadWrite Property TaskEntryPoint As string// You can use this property in JavaScript.
Value
PlatForm::String string string string

The name of an application-defined class that performs the work of a background task.

Remarks

Windows Store app using C++, C#, or Visual Basic The task entry point class must implement the IBackgroundTask interface. The system calls IBackgroundTask::Run when the background task is triggered. In addition, the class must be specified in the <Extensions> section of the application's manifest as <Extension Category="windows.backgroundTasks" EntryPoint="appNamespace.appClassName">.The task entry point class can be implemented in the same process as the application's foreground component (in-proc); however, it cannot run on any of the application's foreground threads because the application might be suspended when the background task is triggered. For best results, implement the class in a separate process (out-of-proc) to decouple it from the application's foreground components and allow the system to manage the application's processes more efficiently.

Windows Store app using JavaScript The entry point for a JavaScript background task is a JavaScript start page. See WebUIBackgroundTaskInstance.current for more information.Note that JavaScript background tasks must call close() to terminate when they are done, so they don't continue to consume the user's memory and battery.

TaskGroup TaskGroup TaskGroup TaskGroup

Gets and sets the group identifier.

public : BackgroundTaskRegistrationGroup TaskGroup { get; set; }public BackgroundTaskRegistrationGroup TaskGroup { get; set; }Public ReadWrite Property TaskGroup As BackgroundTaskRegistrationGroup// You can use this property in JavaScript.
Additional features and requirements
Device family
Windows 10 Creators Update (introduced v10.0.15063.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v4)

Examples

Background Task code sample See scenario 6 for a grouped task example.

Remarks

To reduce collisions with other group identifiers, consider including your domain name in the group identifier. For example: "com.contoso.appname.tasks". Or use the string form of a GUID.

See Also

Methods

AddCondition(IBackgroundCondition) AddCondition(IBackgroundCondition) AddCondition(IBackgroundCondition) AddCondition(IBackgroundCondition)

Adds a condition to a background task.

public : void AddCondition(IBackgroundCondition condition)public void AddCondition(IBackgroundCondition condition)Public Function AddCondition(condition As IBackgroundCondition) As void// You can use this method in JavaScript.
Parameters

Remarks

AddCondition can be called more than once to specify more than one condition for a background task. All specified conditions must be met before the system will schedule the task.

Register() Register() Register() Register()

Registers a background task with the system.

public : BackgroundTaskRegistration Register()public BackgroundTaskRegistration Register()Public Function Register() As BackgroundTaskRegistration// You can use this method in JavaScript.
Returns

Examples

BackgroundTask sample

Remarks

The task must have an event trigger and a task entry point for the Register method to succeed. The system schedules the background task when its trigger event occurs and all of its conditions have been met.

Background task parameter validation

Windows 8 Windows 8 does not validate the parameters set on the BackgroundTaskBuilder object until the system tries to run the background task. If the parameters aren't valid, the background task can't start and an event log entry is created.

Windows 8.1 Starting in Windows 8.1, the parameters used to register the background task are validated at the time of registration. An error is returned if the background task registration fails, allowing the app to determine whether or not the background task is valid. For C# and Visual Basic, task registration errors typically result in specific .NET exceptions being thrown. These exceptions are thrown as first-chance exceptions and should be corrected while you're still developing your code.Existing Windows 8 apps running on Windows 8.1 are subject to this new system behavior, which can cause the app to crash if it can't handle a failed background task registration. (An event log entry will still be generated for the failed background task registration.) As a result, Windows 8 apps that register invalid background tasks should be rewritten to register background tasks correctly and to handle failed background task registration as a caught exception.

SetTrigger(IBackgroundTrigger) SetTrigger(IBackgroundTrigger) SetTrigger(IBackgroundTrigger) SetTrigger(IBackgroundTrigger)

Sets the event trigger for a background task.

public : void SetTrigger(IBackgroundTrigger trigger)public void SetTrigger(IBackgroundTrigger trigger)Public Function SetTrigger(trigger As IBackgroundTrigger) As void// You can use this method in JavaScript.
Parameters

Remarks

In addition to specifying the type of event trigger for a background task, an application must also enable background tasks that use the event trigger type in the <Extensions><Extension><BackgroundTasks> section of its manifest. Valid types include the following:

  • <Task Type="audio"/>
  • <Task Type="timer"/>
  • <Task Type="systemEvent"/>
  • <Task Type="pushNotification"/>
  • <Task Type="realTimeCommunication"/>

If the background task type is not specified or specified incorrectly in the manifest, calls that attempt to use that kind of background task will fail.