2
votes

I've followed the Microsoft guidelines for creating and registering a background task: https://msdn.microsoft.com/en-us/library/windows/apps/mt299100.aspx

I have my main project, which registers the background task with a trigger etc, and another Windows Runtime Component project which holds the code for the background task, and is referenced by my main project.

My code to register the background task is:

bool registered = false;
        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == "BackgroundGPS")
            {
                registered = true;
            }
        }

        if (!registered)
        {
            var builder = new BackgroundTaskBuilder();

            builder.Name = "BackgroundGPS";
            builder.TaskEntryPoint = "BackgroundTasks.BackgroundGPSTask";

            BackgroundExecutionManager.RemoveAccess();
            var x = await BackgroundExecutionManager.RequestAccessAsync();
            var trigger = new TimeTrigger(15, false);
            builder.SetTrigger(trigger);

            BackgroundTaskRegistration task = builder.Register();
            //Here trigger property of 'task' is null.
        }
    }

My background task is never run because it appears the trigger never fires. I've debugged the code, and as per my comment, the trigger property of the BackgroundTaskRegistration variable is null.

Now, I've tried using a SystemTrigger instead of a TimeTrigger and it works, the property is not null. I see that the trigger property is described as 'being null if the trigger type is unsupported'.

Why would TimeTrigger be unsupported? I've called RequestAccessAsync() to get lock-screen access, and it comes back as allowed. I've also added 'Timer' to the task types of the 'Background Task' declaration of my main project's app manifest. By all suggestions from Microsoft the TimeTrigger is the only way to get a background task to run on a timer (MaintenanceTrigger only works when plugged into power).

There are two similar SO questions about this:

Can't register a Time triggered Background Task

BackgroundTaskRegistration trigger property is null

One of which doesn't have any answers on, and the other's suggestions I've already implemented.

Is anyone able to help?

1

1 Answers

1
votes

I think it's a document issue. The backgroundtaskregistration trigger property is also null on my side, but the time trigger does work.

I don't know how you tested it. But doing the following test will show it does work.

  1. Manually uninstall your app to avoid any unexpected behavior.

  2. Define the background task using very simple implementation as following.

    public sealed class BackgroundTask1 : IBackgroundTask
    {
        BackgroundTaskDeferral _deferral;
    
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral = taskInstance.GetDeferral();
    
            await ApplicationData.Current.LocalFolder.CreateFileAsync("test.txt"
            , CreationCollisionOption.ReplaceExisting);
    
    
            _deferral.Complete();
        }
    }
    
  3. In Visual Studio, start debugging your app. After you register the background task, click lifecycle Event tool bar and select the background task as below. This will force the background task to run. Take this oppotunity to check if there is any issue. enter image description here

  4. Go to the following folder and check if you can see the test.txt file exists. Replace the {User name} to your logged user's. In Visual Studio, open project's package.appxmanifest in designer, switch to packaging tab and the last line is {Package family name}, replace the placeholder with it.

C:\Users\{User name}\AppData\Local\Packages\{package family name}\LocalState

  1. To test it out of visual studio, simply wait for 15 mins and repeat step #4.