Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

c# - Timetrigger not firing backgroundtask UWP

I have the same problem as this guy over here: UWP Timetrigger not working but I can't comment the question because my reputation is not high enough, so I'm creating a new question.

As I said, I have the same problem. I registered the background task, but nothing happens. My background task is located in a seperate project (Runtime Component). So that's not the problem. This is the method I made to register the task:

public static BackgroundTaskRegistration Register(string name, string taskEntryPoint, IBackgroundTrigger trigger, IBackgroundCondition condition)
    {
        var foundTask = BackgroundTaskRegistration.AllTasks.FirstOrDefault(x => x.Value.Name == name);
        if (foundTask.Value != null)
            return (BackgroundTaskRegistration)foundTask.Value;

        var builder = new BackgroundTaskBuilder();
        builder.Name = name;
        builder.TaskEntryPoint = taskEntryPoint;
        builder.SetTrigger(trigger);
        if (condition != null)
            builder.AddCondition(condition);

        return builder.Register();
    }

and this is how I call it:

BackgroundTaskRegister.Register(nameof(NotificationTask), $"Epguides.Background.{_backgroundTaskName}", new TimeTrigger(30, true), null);

When I debug my application and use Lifecycle Events in Visual studio to test my background task, everything works fine. So the problem is not the task.

When I inspect the BackgroundTaskRegistration result I see that the property trigger is null. on the MSDN page of BackgroundTaskRegistration.Trigger it says the following

This is not intended for use in your code. For all unsupported trigger types, the value returned by this property is null.

So from what I understand is that TimeTrigger is an unsupported trigger type, because Trigger is null.

This is what is declared in the manifest file Declarations

Is there someone that can explain why it is not working. I'm using version 10.0.10586

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. You can confirm that the your task is registered or not by using powershell. Open powershell with administrative rights, and run 'Get-AppBackgroundTask'. All of registered tasks are listed. If you can't find your task from the list, there are some problems at registration.

  2. Have you add the background task project as a reference to your main app?

  3. Have you call the BackgroundExecutionManager.RequestAccessAsync()? You should call it before registering, from UI thread.

enter image description here

  1. I have a sample app of background task with timetrigger on the store. It's hidden from storefront and search, but you can download it from following link:

https://www.microsoft.com/store/p/ddlgbgtasktrial/9nblggh4s785

This app regist a simple background task with 15min interval timetrigger. This task just output the debugmessage to the logfile. The app shows a log. If it works well, you can see the debug output with about 15min intervals.

I've confirmed that the app works with Win10 desktop 10586.494 and mobile 14393.0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...