0
votes

I'm attempting to set up a Background task to run every 15 minutes from my UWP app, but despite the app registering successfully it isn't being triggered. There's tonnes of material on this around so to quickly sum up what I've done so far:

  1. Created Windows Runtime Component project.
  2. Added BackgroundSync.cs implementing IBackgroundTask.
  3. Referenced WinRT project from my UI project.
  4. Registered background task in package.appmanifest, including Timer and System event trigger boxes checked.
  5. Registered BackgroundSync in App.xaml.cs, code below.
  6. Verified that BackgroundSync is a registered background task in Powershell.
  7. I manually triggered my BackgroundSync task successfully in debug > Lifecycle Events.

Here is my code to register the task:

    private static async Task<BackgroundTaskRegistration> SetupBackgroundSync()
    {
        await BackgroundExecutionManager.RequestAccessAsync();

        var builder = new BackgroundTaskBuilder();

        var TaskName = "BackgroundSync";

        foreach (var t in BackgroundTaskRegistration.AllTasks)
        {
            if (t.Value.Name == TaskName)
            {
                t.Value.Unregister(true);
            }
        }

        builder.Name = TaskName;
        builder.TaskEntryPoint = typeof(Background.BackgroundSync).FullName;
        builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));

        builder.SetTrigger(new TimeTrigger(15, false));

        BackgroundTaskRegistration task = builder.Register();

        return task;
    }

The only issue I can identify is that on creation of the BackgroundTaskRegistration object I can see that the Trigger is null. This shows null for and trigger type other than ApplicationTrigger.

I have seen a post here in which the poster who's answer is accepted states that this null showing in the task object is to be expected, however the solution offered has not helped.

It's worth also stating that when I run RequestAccessAsync() the value I get back is actually BackgroundAccessStatus.AllowedSubjectToSystemPolicy which I feel must have implications. I've seen in some places that RequestAccessAsync() should be called from the UI thread and I'm calling this from App.xaml.cs before registering my tasks.

Does anyone have any other ideas of potential reasons for this?

Update 07-10

Code in my test project for this background task issue:

RegistrationHelper.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;

namespace Tasks
{
    public sealed class RegistrationHelper
    {

        public async Task<BackgroundTaskRegistration> RegisterTasks()
        {
            //Returns AllowedSubjectToSystemPolicy
            await BackgroundExecutionManager.RequestAccessAsync();

            var builder = new BackgroundTaskBuilder();

            var TaskName = "BackgroundTask1";

            foreach (var t in BackgroundTaskRegistration.AllTasks)
            {
                if (t.Value.Name == TaskName)
                {
                    t.Value.Unregister(true);
                }
            }

            builder.Name = TaskName;
            builder.TaskEntryPoint = typeof(BackgroundTask1).FullName;
            builder.AddCondition(new     SystemCondition(SystemConditionType.InternetAvailable));

            builder.SetTrigger(new TimeTrigger(15, false));

            BackgroundTaskRegistration task = builder.Register();

            return task;

        }

    }
}

Called in my App.xaml.cs here.

    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;

        Tasks.RegistrationHelper registrationhelper = new Tasks.RegistrationHelper();

        var task = registrationhelper.RegisterTasks();
    }

My background task is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;

namespace Tasks
{
    public sealed class BackgroundTask1 : IBackgroundTask
    {

        public void Run(IBackgroundTaskInstance taskInstance)
        {

            while (true) { }

        }

    }
}

Solution structure. BackgroundTaskTest is UWP Blank app. Tasks is a UWP Windows Runtime Component:

Solution structure. BackgroundTaskTest is UWP Blank app. Tasks is a UWP Windows Runtime Component.

And lastly, my PackageManifest:

<Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="Tasks.BackgroundTask1">
      <BackgroundTasks>
        <Task Type="systemEvent" />
        <Task Type="timer" />
      </BackgroundTasks>
    </Extension>
  </Extensions>
1
I was about to post same question. My TimeTrigger also registered fine but does not fire. Looking forward for the answer.AVK
Fingers crossed... Does your trigger show as null in your debugger also?JonnyKnottsvill
yes. Its either we are missing something in documentation or documentation is not up to date or its missing few steps.AVK
I made a demo, and wait for 15 mins and the trigger triggers correctly. Could you please share a demo that can reproduce this problem?Elvis Xia - MSFT
@ElvisXia-MSFT what would be the best way of sharing that with you? I have a dedicated test project that has the same issue.JonnyKnottsvill

1 Answers

0
votes

It could be not the solution for you, but, sometimes, my TimeTrigger tasks also not starts and in this case helps next steps

  1. Disable Background access to the app in windows settings
  2. Restart PC
  3. Enable access
  4. Start app to register background tasks

Also your provided code has several problematic points that could cause issues

  1. Registration of background tasks inside App.xaml.cs. The better place to put it Loaded event handler of main app Page
  2. "while (true) { }" code inside task, replace it, as example, with simple toast notification, for you it would be much easier to test task start time
  3. There is no need to unregister task,if it already registered just work with available instance