Run Background Task at specific time - UWP

ぃ、小莉子 提交于 2020-01-06 19:57:05

问题


I have asked this Before i know.. but i cant get a straight answer and can't find a good one on google either.

I want to send a Toast Notification to the user once a day, if some requirements are met, otherwise check the next day. I have a SQLite db that i check against. I have a Background Task that should work but it checks as soon as i start the app but never again.. its really hard to debug this as well.. :/

I have this:

 public async void RegisterBackgroundTask()
    {
        var tommorowMidnight = DateTime.Today.AddDays(1);
        var timeTilMidnight = tommorowMidnight - DateTime.Now;
        var minutesTilMidnight = (uint)timeTilMidnight.TotalMinutes;

        var task = RegisterBackgroundTask("TaskBuilder",
                                          "TimeTriggeredTask",
                                          new TimeTrigger(15, false), //new TimeTrigger(minutesTilMidnight, false),
                                          null);
        await task;
                CheckPremieres(); //My method that i want to check only once a day at 00:00


    }

I have also tried this:

 public async void RegisterBackgroundTask()
    {
        var tommorowMidnight = DateTime.Today.AddDays(1);
        var timeTilMidnight = tommorowMidnight - DateTime.Now;
        var minutesTilMidnight = (uint)timeTilMidnight.TotalMinutes;

        var task = RegisterBackgroundTask("TaskBuilder",
                                          "TimeTriggeredTask",
                                          new TimeTrigger(15, false), //new TimeTrigger(minutesTilMidnight, false),
                                          null);
        await task.ContinueWith((x) =>
            {
                CheckPremieres(); //My method that i want to check only once a day at 00:00
            });

    }

The 15min is only for testing, the comment out is the real code.

My problem is that this dont work as it is now, because as I said it only check once.. then nothing when the program is running, nothing when the program is put to the background.. What am i doing wrong??

The methods above is in my notificationhandler class along with RegisterBackgroundTask method.. then I have a taskbuilder class.. but that one is as it is from the MS GitHub example..

Is there any other info you guys need to beable to help me?? I Think I will soon bash my head in the screen...!

Edit: Have I misunderstood the whole thing.. is it in Run at the TaskBuilder class i should run my method??

I simple example would be much appreciated..


回答1:


Add something like this to your background task:

var task = RegisterBackgroundTask("TaskBuilder",
                                      "TimeTriggeredTask",
                                      TimeTrigger(1440, true),//1440 for the number of minutes in a day
                                      null);

The code you have commented out should cause the background task to trigger at midnight, which you said you have successfully tested. From there you need to register the background task to reoccur every 24 hours. The secondary argument for the time trigger indicates whether it should repeat or not.

Double check that the task isn't scheduled in the background task before running it and you should be good to go.



来源:https://stackoverflow.com/questions/34556127/run-background-task-at-specific-time-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!