后台任务的多个实例
Multiple instances of a background task
如何在 UWP-App 中启动同一后台任务的多个实例?
我在本教程中注册它:https://msdn.microsoft.com/en-us/library/windows/apps/mt299100.aspx?f=255&MSPPError=-2147217396
我第一次这样做时成功了,但是当我用不同的名称注册第二个任务时,出现异常:
System.Exception: 配额不足,无法处理此命令。 (HRESULT 异常:0x80070718)
您遇到的错误 general error 与系统上的虚拟内存有关。
按照您提到的教程,每个任务只会注册一次,除非您更改以下步骤(注册过程的第一步):
var taskRegistered = false;
var exampleTaskName = "ExampleBackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == exampleTaskName)
{
taskRegistered = true;
break;
}
}
BackgroundTaskRegistration.AllTasks 的重点是枚举应用程序的所有已注册后台任务。
这意味着一个任务可以注册一次、两次或与您一样多want/need(尽管我现在想不出您想要这样的任何场景)。
因此,为了注册多个实例,您需要做的就是为每个要注册的实例调用如下方法:
private BackgroundTaskRegistration RegisterTask(
Type taskType,
SystemTriggerType systemTriggerType,
SystemConditionType systemConditionType = SystemConditionType.Invalid)
{
var builder = new BackgroundTaskBuilder();
/// A string identifier for the background task.
builder.Name = taskType.Name;
/// The entry point of the task.
/// This HAS to be the full name of the background task: {Namespace}.{Class name}
builder.TaskEntryPoint = taskType.FullName;
/// The specific trigger event that will fire the task on our application.
builder.SetTrigger(new SystemTrigger(systemTriggerType, false));
/// A condition for the task to run.
/// If specified, after the event trigger is fired, the OS will wait for
/// the condition situation to happen before executing the task.
if (systemConditionType != SystemConditionType.Invalid)
{
builder.AddCondition(new SystemCondition(systemConditionType));
}
/// Register the task and returns the registration output.
return builder.Register();
}
请记住,在调用 BackgroundExecutionManager.RequestAccessAsync() 方法时,系统或用户可能会拒绝您的应用程序访问后台任务系统。
另一个可能阻碍您的问题是,如果系统资源耗尽,它可能不会注册或执行后台任务,以便为更重要的任务节省资源。
如何在 UWP-App 中启动同一后台任务的多个实例?
我在本教程中注册它:https://msdn.microsoft.com/en-us/library/windows/apps/mt299100.aspx?f=255&MSPPError=-2147217396
我第一次这样做时成功了,但是当我用不同的名称注册第二个任务时,出现异常:
System.Exception: 配额不足,无法处理此命令。 (HRESULT 异常:0x80070718)
您遇到的错误 general error 与系统上的虚拟内存有关。
按照您提到的教程,每个任务只会注册一次,除非您更改以下步骤(注册过程的第一步):
var taskRegistered = false;
var exampleTaskName = "ExampleBackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == exampleTaskName)
{
taskRegistered = true;
break;
}
}
BackgroundTaskRegistration.AllTasks 的重点是枚举应用程序的所有已注册后台任务。
这意味着一个任务可以注册一次、两次或与您一样多want/need(尽管我现在想不出您想要这样的任何场景)。
因此,为了注册多个实例,您需要做的就是为每个要注册的实例调用如下方法:
private BackgroundTaskRegistration RegisterTask(
Type taskType,
SystemTriggerType systemTriggerType,
SystemConditionType systemConditionType = SystemConditionType.Invalid)
{
var builder = new BackgroundTaskBuilder();
/// A string identifier for the background task.
builder.Name = taskType.Name;
/// The entry point of the task.
/// This HAS to be the full name of the background task: {Namespace}.{Class name}
builder.TaskEntryPoint = taskType.FullName;
/// The specific trigger event that will fire the task on our application.
builder.SetTrigger(new SystemTrigger(systemTriggerType, false));
/// A condition for the task to run.
/// If specified, after the event trigger is fired, the OS will wait for
/// the condition situation to happen before executing the task.
if (systemConditionType != SystemConditionType.Invalid)
{
builder.AddCondition(new SystemCondition(systemConditionType));
}
/// Register the task and returns the registration output.
return builder.Register();
}
请记住,在调用 BackgroundExecutionManager.RequestAccessAsync() 方法时,系统或用户可能会拒绝您的应用程序访问后台任务系统。
另一个可能阻碍您的问题是,如果系统资源耗尽,它可能不会注册或执行后台任务,以便为更重要的任务节省资源。