Safe Usx
Safe Usx

Reputation: 3

why is my background task only running once?

I am using a UWP project with a background task that is triggered by the internet being available. Once triggered, a toast notification is displayed.

The problem is that the background task seems to only run once after launching the UWP application. it even works after closing the application and restarting my computer as long as I haven't triggered it before doing so, but only if it is untriggered before restarting.

What am I doing wrong? am I missing something or misusing the background task?

For clarification, I want it to send a notification every time the internet is connected. The background task should run independent of the main application.

Below is the code for the background task:

namespace AppService
{
    public sealed class testNoteUpdaterTask : IBackgroundTask
    {
        BackgroundTaskDeferral _deferral; // Note: defined at class scope so that we can mark it complete inside the OnCancel() callback if we choose to support cancellation

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // Get a deferral so that the service isn't terminated.
            _deferral = taskInstance.GetDeferral(); 

            // Construct the content
            new ToastContentBuilder()
                .AddArgument("action", "testNote")
                .AddArgument("conversationId", 9813)
                .AddText("Program update avaliable for testNote")

            // Buttons
            .AddButton(new ToastButton()
                .SetContent("testNote stuff")
                .AddArgument("action", "open")
                .SetBackgroundActivation())

            .Show(); 
            
            _deferral.Complete();

        }
    }
}

And here is the code which I use to register the background task inside the main UWP application:

public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint, string taskName, IBackgroundTrigger trigger, 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;
    builder.TaskEntryPoint = taskEntryPoint;
    builder.SetTrigger(trigger);

    if (condition != null) {
        builder.AddCondition(condition);
    }

    BackgroundTaskRegistration task = builder.Register();
    return task;
}

public MainPage()
{
    this.InitializeComponent();
    RegisterBackgroundTask("AppService.testNoteUpdaterTask", "testNoteUpdaterX", new SystemTrigger(SystemTriggerType.InternetAvailable, true));
}

Upvotes: 0

Views: 222

Answers (1)

Roy Li - MSFT
Roy Li - MSFT

Reputation: 8666

I checked your code. It seems that when you are registering the SystemTrigger here:

new SystemTrigger(SystemTriggerType.InternetAvailable, true)

You are setting the oneShot parameter as true, which means the system event trigger will be used only once. Please set this value to false if you want the system event trigger to be used every time the event occurs.

More information here:SystemTrigger(SystemTriggerType, Boolean) Constructor.

Please use the following code:

 new SystemTrigger(SystemTriggerType.InternetAvailable, false)

You could also take a look at the official background task sample here: BackgroundTask Sample line 166.

Upvotes: 1

Related Questions