Multiple instances of a background task

0

How can I start multiple instances of the same background task in an UWP-App?

I register it like in this tutorial: https://msdn.microsoft.com/en-us/library/windows/apps/mt299100.aspx?f=255&MSPPError=-2147217396

The first time I do this it works but when I register the second task with a different name i get an exception:

System.Exception: Not enough quota is available to process this command. (Exception from HRESULT: 0x80070718)

c#
background
win-universal-app
background-process
asked on Stack Overflow Feb 18, 2016 by TableCreek • edited Feb 18, 2016 by TableCreek

1 Answer

1

The error that you're getting is a general error related to virtual memory on the system.

Following the tutorial you mentioned will register each task only once, unless you change the following step (first step of the registration process):

var taskRegistered = false;
var exampleTaskName = "ExampleBackgroundTask";

foreach (var task in BackgroundTaskRegistration.AllTasks)
{
    if (task.Value.Name == exampleTaskName)
    {
        taskRegistered = true;
        break;
    }
}

The whole point of the BackgroundTaskRegistration.AllTasks is to enumerate all the applicatio's registered background tasks.

This means that a task can be registered once, twice or as much as you want/need (although I can't think of any scenario in which you would like such a thing right now).

So in order to register multiple instances, all you need to do is call a method such as the following one for every instance you want to register:

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();
}

Just keep in mind that the system or the user may deny access for your application to the background task system when calling the BackgroundExecutionManager.RequestAccessAsync() method.

A different problem that may be blocking you is that, if the system runs out of resources, it may not register or execute the background tasks in order to save thos resources for more important tasks.

answered on Stack Overflow Feb 18, 2016 by Nahuel Ianni

User contributions licensed under CC BY-SA 3.0