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:
- Created Windows Runtime Component project.
- Added BackgroundSync.cs implementing
IBackgroundTask
. - Referenced WinRT project from my UI project.
- Registered background task in package.appmanifest, including Timer and System event trigger boxes checked.
- Registered BackgroundSync in App.xaml.cs, code below.
- Verified that BackgroundSync is a registered background task in Powershell.
- 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:
And lastly, my PackageManifest:
<Extensions>
<Extension Category="windows.backgroundTasks" EntryPoint="Tasks.BackgroundTask1">
<BackgroundTasks>
<Task Type="systemEvent" />
<Task Type="timer" />
</BackgroundTasks>
</Extension>
</Extensions>