在特定时间运行后台任务 - UWP [英] Run Background Task at specific time - UWP

查看:71
本文介绍了在特定时间运行后台任务 - UWP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在知道之前就问过这个问题..但我无法得到直接的答案,也无法在谷歌上找到好的答案.

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

我想每天向用户发送一次 Toast 通知,如果满足某些要求,否则第二天检查.我有一个我检查的 SQLite 数据库.我有一个应该可以工作的后台任务,但它会在我启动应用程序后立即检查,但再也不会检查了.. 调试它也真的很难.. :/

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.. :/

我有这个:

 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


    }

我也试过这个:

 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
            });

    }

15min 仅用于测试,注释掉的是真正的代码.

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??

上述方法与 RegisterBackgroundTask 方法一起在我的通知处理程序类中.. 然后我有一个 taskbuilder 类.. 但那个是来自 MS GitHub 示例的那个..

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...!

我是否误解了整件事..它是在 TaskBuilder 类中运行我应该运行我的方法吗?

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..

推荐答案

将这样的内容添加到您的后台任务:

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);

你注释掉的代码应该会导致后台任务在午夜触发,你说你已经成功测试了.从那里您需要注册后台任务以每 24 小时重新发生一次.时间触发器的第二个参数指示它是否应该重复.

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.

这篇关于在特定时间运行后台任务 - UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆