重要的应用程序接口(API)
了解如何设置用于控制后台任务何时运行的条件。
有时,后台任务需要满足某些条件才能使后台任务成功。 注册后台任务时,可以指定 SystemConditionType 指定的一个或多个条件。 触发器被激活后,将检查条件。 然后,后台任务将排队,但只有在满足所有所需条件后才会运行。
通过防止任务不必要地运行,在后台任务上设置条件可以节省电池电量和 CPU。 例如,如果后台任务在计时器上运行并且需要 Internet 连接,请在注册任务之前将 InternetAvailable 条件添加到 TaskBuilder。 这有助于防止任务在计时器运行 且 Internet 可用时,才运行后台任务,从而避免不必要的使用系统资源和电池使用时间。
还可以通过在同一 TaskBuilder上多次调用 AddCondition 来组合多个条件。 请注意不要添加冲突的条件,如 UserPresent 和 UserNotPresent。
创建 SystemCondition 对象
本主题假定你已具有与应用关联的后台任务,并且你的应用已包含用于创建一个名为 taskBuilder的 BackgroundTaskBuilder 对象的代码。 如果需要先创建后台任务,请参阅 创建和注册进程内后台任务 或 创建和注册进程外后台任务。
本主题适用于进程外运行的后台任务,以及与前台应用在同一进程中运行的后台任务。
在添加条件之前,请创建 SystemCondition 对象来表示必须生效才能运行后台任务的条件。 在构造函数中,指定 SystemConditionType 枚举值必须满足的条件。
以下代码创建一个 SystemCondition 对象,该对象指定 InternetAvailable 条件:
SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
Windows::ApplicationModel::Background::SystemCondition internetCondition{
    Windows::ApplicationModel::Background::SystemConditionType::InternetAvailable };
SystemCondition ^ internetCondition = ref new SystemCondition(SystemConditionType::InternetAvailable);
将 SystemCondition 对象添加到后台任务
若要添加条件,请在 
以下代码使用 taskBuilder 用于添加 InternetAvailable 条件。
taskBuilder.AddCondition(internetCondition);
taskBuilder.AddCondition(internetCondition);
taskBuilder->AddCondition(internetCondition);
注册后台任务
现在,可以使用 Register 方法注册后台任务,在满足指定条件之前,后台任务将不会启动。
以下代码注册任务并存储生成的 BackgroundTaskRegistration 对象:
BackgroundTaskRegistration task = taskBuilder.Register();
Windows::ApplicationModel::Background::BackgroundTaskRegistration task{ recurringTaskBuilder.Register() };
BackgroundTaskRegistration ^ task = taskBuilder->Register();
注释
后台任务注册参数在注册时会被验证。 如果任何注册参数无效,则返回错误。 确保应用程序能够有效处理后台任务注册失败的情况——如果在尝试注册任务后应用程序依赖于拥有有效的注册对象,那么它可能会崩溃。
在后台任务中设置多个条件
若要添加多个条件,应用对 AddCondition 方法进行多次调用。 这些调用必须在任务注册生效之前进行。
注释
请注意不要将冲突条件添加到后台任务。
以下代码片段显示了创建和注册后台任务的上下文中的多个条件。
// Set up the background task.
TimeTrigger hourlyTrigger = new TimeTrigger(60, false);
var recurringTaskBuilder = new BackgroundTaskBuilder();
recurringTaskBuilder.Name           = "Hourly background task";
recurringTaskBuilder.TaskEntryPoint = "Tasks.ExampleBackgroundTaskClass";
recurringTaskBuilder.SetTrigger(hourlyTrigger);
// Begin adding conditions.
SystemCondition userCondition     = new SystemCondition(SystemConditionType.UserPresent);
SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
recurringTaskBuilder.AddCondition(userCondition);
recurringTaskBuilder.AddCondition(internetCondition);
// Done adding conditions, now register the background task.
BackgroundTaskRegistration task = recurringTaskBuilder.Register();
// Set up the background task.
Windows::ApplicationModel::Background::TimeTrigger hourlyTrigger{ 60, false };
Windows::ApplicationModel::Background::BackgroundTaskBuilder recurringTaskBuilder;
recurringTaskBuilder.Name(L"Hourly background task");
recurringTaskBuilder.TaskEntryPoint(L"Tasks.ExampleBackgroundTaskClass");
recurringTaskBuilder.SetTrigger(hourlyTrigger);
// Begin adding conditions.
Windows::ApplicationModel::Background::SystemCondition userCondition{
    Windows::ApplicationModel::Background::SystemConditionType::UserPresent };
Windows::ApplicationModel::Background::SystemCondition internetCondition{
    Windows::ApplicationModel::Background::SystemConditionType::InternetAvailable };
recurringTaskBuilder.AddCondition(userCondition);
recurringTaskBuilder.AddCondition(internetCondition);
// Done adding conditions, now register the background task.
Windows::ApplicationModel::Background::BackgroundTaskRegistration task{ recurringTaskBuilder.Register() };
// Set up the background task.
TimeTrigger ^ hourlyTrigger = ref new TimeTrigger(60, false);
auto recurringTaskBuilder = ref new BackgroundTaskBuilder();
recurringTaskBuilder->Name           = "Hourly background task";
recurringTaskBuilder->TaskEntryPoint = "Tasks.ExampleBackgroundTaskClass";
recurringTaskBuilder->SetTrigger(hourlyTrigger);
// Begin adding conditions.
SystemCondition ^ userCondition     = ref new SystemCondition(SystemConditionType::UserPresent);
SystemCondition ^ internetCondition = ref new SystemCondition(SystemConditionType::InternetAvailable);
recurringTaskBuilder->AddCondition(userCondition);
recurringTaskBuilder->AddCondition(internetCondition);
// Done adding conditions, now register the background task.
BackgroundTaskRegistration ^ task = recurringTaskBuilder->Register();
注解
注释
为后台任务选择条件,使其仅在需要时运行,并且不应在不需要时运行。 有关不同后台任务条件的说明,请参阅 SystemConditionType。