0

I have a UWP application where I want to add background task support for doing certain things while my application is in background. I am doing exactly as it is mentioned here: https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task

I have one separate project for Background Tasks and in my package manifest file I have declared that my app uses background tasks (but "timer" task since I am using TimerTrigger). Code:

BackgroundTaskBuilder backgroundTaskBuilder = new BackgroundTaskBuilder { Name = "NotificationUpdater", TaskEntryPoint = "NamespaceOfMyBackgroundTaskInterfaceImplementation.BackgroundTask"};

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

BackgroundTaskRegistration backgroundTaskRegistration = backgroundTaskBuilder.Register();

Now, when I launch my app (via Visual Studio), and use Lifecycle Events dropdown to suspend my app, it never executes the Run() method in the BackgroundTask class (implementation of IBackgroundTask interface). Code inside BackgroundTask class:

namespace NamespaceOfMyBackgroundTaskInterfaceImplementation
{
    public sealed class BackgroundTask : IBackgroundTask
    {
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //Code to run in the background
        }
    }
}
tavier
  • 1,744
  • 4
  • 24
  • 53
  • Have you declared an entry point in manifest file, added a reference in your main project and made your BTask a runtime component? You may also take a look [at the steps here at answer](http://stackoverflow.com/a/24076237/2681948). – Romasz Apr 25 '16 at 06:32
  • Yes, I have declared the entrypoint in the manifest file, added a reference in my app project. My BakgroundTask is Universal Windows class library actually since I am developing a UWP app. – tavier Apr 25 '16 at 07:56
  • Ah, I think that's what I am doing wrong. – tavier Apr 25 '16 at 08:05
  • 1
    Change it to *Windows Runtime Component* and probably will run. – Romasz Apr 25 '16 at 08:10
  • It does indeed work after changing to Windows Runtime. Thanks a ton. – tavier Apr 25 '16 at 08:18

1 Answers1

1

As it turned out in the conversation, the problem was in wrong declaration of project type. In should be a Windows Runtime Component.

For the followers, please take a look at this answer, which describes the steps. It answers to Silverlight 8.1, but the process and steps are the same in WinRT and UWP.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Hey@Romasz just wanted to check if you have any thoughts on this one here: http://stackoverflow.com/questions/36837912/background-agent-in-uwp – tavier Apr 25 '16 at 10:32
  • @AshishAgrawal No - no thoughts on that. Can you say from where you have those information? – Romasz Apr 25 '16 at 11:50
  • I kind of figured that out by running some validations in both the platforms. – tavier Apr 25 '16 at 13:20
  • @AshishAgrawal I would stick to the docs rather than what I've observed when running app myself. – Romasz Apr 26 '16 at 04:59
  • Yes, but I think they have these constraints in Windows phone 10 as well: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/fd5f3fd7-49a9-4b4a-9b7e-13abf03016b1/background-agent-in-uwp?forum=wpdevelop – tavier Apr 26 '16 at 12:26
  • I noticed one strange thing about the background agent I implemented. So it all works fine if I use the lifecycle events dropdown to suspend the app and trigger the background task from the dropdown, but if I test it in by closing my app or suspending it, it does not trigger the background agent at all (even if I wait for two long hours). – tavier Apr 26 '16 at 12:28
  • @AshishAgrawal Try to stick to [guidelines](https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/guidelines-for-background-tasks). – Romasz Apr 26 '16 at 19:25
  • Yes I will try this out. Thanks for the guidelines. – tavier Apr 27 '16 at 05:37