Search code examples
triggersuwpbackground-service

UWP trying to run background service throwing exception


I am trying to run background service in UWP application. I am first checking if application has background permission. If yes then I am registering the service for running.

This code was working fine until I updated Visual Studio along with Windows 10 SDK to Creators Update version. Now I can't figure out if this update changes things for registering background service.

using System;
using Windows.ApplicationModel.Background;
using BackgroundService;
using SampleApp.Config;

namespace SampleApp.Background
{
    class BackgroundClass
    {
        LocalConfig LC = new LocalConfig();

        public async void RequestBackgroundAccess()
        {
            var result = await BackgroundExecutionManager.RequestAccessAsync();

            switch (result)
            {
                case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
                    break;
                case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
                    break;
                case BackgroundAccessStatus.Denied:
                    break;
                case BackgroundAccessStatus.Unspecified:
                    break;
            }
        }

        public async void RegisterBackgroundSync()
        {
            var trigger = new ApplicationTrigger();
            var condition = new SystemCondition(SystemConditionType.InternetAvailable);

            if (!LC.BackgroundSyncStatusGET())
            {
                var task = new BackgroundTaskBuilder
                {
                    Name = nameof(BackgroundSync),
                    CancelOnConditionLoss = true,
                    TaskEntryPoint = typeof(BackgroundSync).ToString(),
                };

                task.SetTrigger(trigger);
                task.AddCondition(condition);
                task.Register();

                LC.BackgroundSyncStatusSET(true);
            }

            await trigger.RequestAsync(); //EXCEPTION HAPPENS AT THIS LINE
        }

        public void RegisterBackgroundService(uint time)
        {
            var taskName = "BackgroundService";

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

            if(time != 0)
            {
                var trigger = new TimeTrigger(time, false);
                var condition = new SystemCondition(SystemConditionType.InternetAvailable);

                var task = new BackgroundTaskBuilder
                {
                    Name = nameof(BackgroundService),
                    CancelOnConditionLoss = true,
                    TaskEntryPoint = typeof(BackgroundService).ToString(),
                };

                task.SetTrigger(trigger);
                task.AddCondition(condition);
                task.Register();
            }
        }
    }
}

Now while requesting I am checking if background service is registered keeping issues for re-registration. I am getting following exception


System.Runtime.InteropServices.COMException occurred

HResult=0x80004005

Message=Error HRESULT E_FAIL has been returned from a call to a COM component.

Source=Windows   StackTrace:   

at Windows.ApplicationModel.Background.ApplicationTrigger.RequestAsync()   

at SampleApp.Background.BackgroundClass.d__2.MoveNext()


Please Help


Solution

  • Had this same problem, was in my Windows 10 Privacy Settings.

    System Settings => Privacy Settings

    In the left-hand menu choose Background apps.

    Check to make sure your app hasn't been blocked from running background tasks.