UWP Background Task with ApplicationTrigger - Register Problem

Viewed 35

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

1 Answers

First, you might have some misunderstanding about background tasks in UWP. There are two types of background tasks: Out-of-process background tasks and In-process background tasks.

  • Out-of-process background tasks: It is implemented as lightweight classes that implement the IBackgroundTask interface that the OS runs in a separate process (backgroundtaskhost.exe).
  • In-process background tasks: the app and its background process run in the same process

The taskEntryPoint property is for Out-of-process background tasks, it will set the class(in another project- a Windows Runtime Component) that performs the work of the background task.

In-process background tasks don't need to set this property because the background task will be handled in the OnBackgroundActivated() even of the App.Xaml.cs.

Back to your scenario, you are trying to set the taskEntryPoint property for your background task. So this should be an Out-of-process background tasks. You need to create a Windows Runtime Component project in your solution and put the BackupBackgroundTask class in that project. Of course, you could choose not to set this property but you will need to handle the background activity code in OnBackgroundActivated().

You could refer to these two documents about how to create Out-of-process background tasks and In-process background tasks. Create and register an out-of-process background task and Create and register an in-process background task.

Related