Bernd Morgeneyer
Bernd Morgeneyer

Reputation: 109

UWP background tasks: Does it run indefinitely? How to check if it is already running?

From a UWP app I start the following UWP background task using BackgroundTaskBuilder:

namespace Background.UWP
{
    public sealed class BackgroundTask : IBackgroundTask
    {
        private WatchLoop _watchLoop;

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            ApplicationTriggerDetails details = taskInstance.TriggerDetails as ApplicationTriggerDetails;
            var arg = details.Arguments["MyArg"].ToString();

            // Start an infinite loop that periodically does some checks 
            _watchLoop = new WatchLoop(arg, new ToastNotification());
            _watchLoop.Start();    
        }    
    }
}

First question: Does this background task run indefinitely if not cancelled by certain check conditions?

Second question: In my UWP app, how can I check if this background task is already running? I want to avoid starting a second background task. Is there a global handle of this task that I can store (serialized) in global storage (e.g. database) and use in the next instance of the UWP app?

Upvotes: 1

Views: 69

Answers (2)

Bernd Morgeneyer
Bernd Morgeneyer

Reputation: 109

I solved this problem as follows:

Before registering and starting my background task I do a check like in Junjie Zhu's example. I register and start the background task only if it is not yet registered.

I then put a try ... finally around my _watchLoop.Start() loop and in the finally block I put some code to unregister the background task.

This should work in normal operation. However, if I stop all processes in VS Debugger, the background task stops immediately without reaching the finally block. For this purpose I declare a compile switch that during development always unregisters my background task before starting.

Upvotes: 0

Junjie Zhu - MSFT
Junjie Zhu - MSFT

Reputation: 2979

First question: Does this background task run indefinitely if not cancelled by certain check conditions?

According to the document Run background tasks indefinitely. Once a background task has started, whether by a trigger or an app service call, once it takes a deferral on the BackgroundTaskInstance provided by the Run method, it can run indefinitely. If the app is set to Managed By Windows, then it still may have an energy quota applied to it, and its background tasks will not activated when Battery Saver is active. 

Second question: In my UWP app, how can I check if this background task is already running? I want to avoid starting a second background task. Is there a global handle of this task that I can store (serialized) in global storage (e.g. database) and use in the next instance of the UWP app?

The list of background tasks currently registered by the application is kept in the BackgroundTaskRegistration.AllTasks property. It is recommended to check this document.

var taskRegistered = false;
var exampleTaskName = "ExampleBackgroundTask";
 
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
    if (task.Value.Name == exampleTaskName)
    {
        taskRegistered = true;
        break;
    }
} 

Upvotes: 0

Related Questions