I'm trying to add a out-of-process background task to a UWP app. This should be triggered by an application trigger. When trying to register, I get the following error message:
Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
I called
await Windows.ApplicationModel.Background.BackgroundExecutionManager.RequestAccessAsync()
earlier.
I've also tried declaring them in the AppxManifest, but that didn't work either, and I don't know what task type to specify either.
public static async Task RegisterBackupTaskAsync()
{
var requestStatus = await Windows.ApplicationModel.Background.BackgroundExecutionManager.RequestAccessAsync();
if (requestStatus != BackgroundAccessStatus.AlwaysAllowed)
{
await DialogHelper.ShowMessageDialogAsync("Bitte erlauben Sie der App im Hintergrund augeführt zu werden, sonst kommt es zu Problemen mit dem Backup", "Achtung");
return;
}
//var condition = new SystemCondition(SystemConditionType.UserPresent);
AppTriggerBackupTask = new ApplicationTrigger();
var task = await RegisterBackgroundTaskAsync(typeof(BackupBackgroundTask).ToString(), nameof(BackupBackgroundTask), AppTriggerBackupTask);
}
public static async Task<BackgroundTaskRegistration> RegisterBackgroundTaskAsync(string taskEntryPoint, string taskName, ApplicationTrigger trigger = null, IBackgroundCondition condition = null)
{
// Check for existing registrations of this background task.
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == taskName)
{
// The task is already registered.
return (BackgroundTaskRegistration)(cur.Value);
}
}
// Register the background task.
var builder = new BackgroundTaskBuilder();
builder.Name = taskName;
// in-process background tasks don't set TaskEntryPoint
if (taskEntryPoint != null && taskEntryPoint != String.Empty)
{
builder.TaskEntryPoint = taskEntryPoint;
}
builder.SetTrigger(trigger);
if (condition != null)
{
builder.AddCondition(condition);
}
try
{
BackgroundTaskRegistration task = builder.Register();
return task;
}
catch (Exception ex)
{
await DialogHelper.ShowErrorDialogAsync(ex);
return null;
}
}
RegisterBackupTaskAsync is called in the OnLaunched Method in App.xaml.cs