【问题标题】:UWP Background Task Timer not firingUWP 后台任务计时器未触发
【发布时间】:2017-03-27 23:11:21
【问题描述】:

我目前正在开发一个带有后台任务的应用程序,该任务应在用户设置的时间触发。例如,用户选择“01:45 PM”,应用程序正在计算从现在到那个时间的分钟数,并注册一个后台任务与时间触发器。不幸的是,后台任务根本没有触发。有时它只是在我启动计算机后触发。感谢您的任何建议,因为我一周后无法解决此问题。

我已经通过 VisualStudio 启动后台任务调试,所以问题不在 BackgroundTask.cs 文件中。

这是我的代码:

  • 注册后台任务:

    //I set the time to 15 minutes to see if this would work. It didn't...
    var trigger = new TimeTrigger(15, true);
    BackgroundTaskHelper.RegisterBackgroundTask("BackgroundTask.BackgroundTask", "BackgroundTask", trigger, null);
    
  • 后台任务注册方法:

    public static async void RegisterBackgroundTask(string taskEntryPoint, string taskName, IBackgroundTrigger trigger, IBackgroundCondition condition)
    {
        foreach (var cur in BackgroundTaskRegistration.AllTasks)
        {
            if (cur.Value.Name == taskName)
            {
                cur.Value.Unregister(true);
            }
        }            
        var builder = new BackgroundTaskBuilder();
        builder.Name = taskName;
        builder.TaskEntryPoint = taskEntryPoint;
        builder.SetTrigger(trigger);
    
        if (condition != null)
        {
            builder.AddCondition(condition);
        }
    
        await BackgroundExecutionManager.RequestAccessAsync();
        var task = builder.Register();
    }
    
  • Package.appxmanifest Package.appxmanifest, Image

感谢您的帮助!

【问题讨论】:

  • 这可能有很多问题。我经常想到的两件事是:后台任务运行多长时间(超过 30 秒是标准 BGTask 的问题),第二:此任务的 CPU 使用率有多高以及 CPU 有多高用法,什么时候应该运行?这也可能会有所帮助:docs.microsoft.com/en-us/windows/uwp/launch-resume/…
  • 目前只发送一个toast通知,所以持续时间不到30秒,cpu使用率很低。
  • 它在触发时的 CPU 使用率是多少?因为如果主机的 CPU 使用率很高,有时任务不会触发。

标签: c# background uwp


【解决方案1】:

创建一个新的TimeTrigger。第二个参数 OneShot 指定后台任务是只运行一次还是定期运行。如果 OneShot 设置为 true,则第一个参数 (FreshnessTime) 指定在调度后台任务之前要等待的分钟数。如果 OneShot 设置为 false,FreshnessTime 指定 background task 运行的频率。

如果 FreshnessTime 设置为 15 分钟且 OneShot 为 true,则任务将计划在其注册时间后 15 到 30 分钟之间运行一次。如果将其设置为 25 分钟并且 OneShot 为 true,则任务将被安排在其注册后的 25 到 40 分钟之间运行一次。

所以TimeTrigger 不适合您的场景。在 Windows 10 UWP 中,警报只是带有“警报”场景的 Toast 通知。要在特定时间出现警报,您可以使用预定的 Toast 通知。

以下代码是如何安排警报在某个时间出现。详情请参考Quickstart: Sending an alarm in Windows 10。随附的sample application 是一个简单的快速启动警报应用程序。

DateTime alarmTime = DateTime.Now.AddMinutes(1);

// Only schedule notifications in the future
// Adding a scheduled notification with a time in the past
// will throw an exception.
if (alarmTime > DateTime.Now.AddSeconds(5))
{
    // Generate the toast content (from previous steps)
    ToastContent toastContent = GenerateToastContent();

    // Create the scheduled notification
    var scheduledNotif = new ScheduledToastNotification(
        toastContent.GetXml(), // Content of the toast
        alarmTime // Time we want the toast to appear at
        );

    // And add it to the schedule
    ToastNotificationManager.CreateToastNotifier().AddToSchedule(scheduledNotif);
}

【讨论】:

  • 谢谢!这很有趣!是否有可能在预定时间之后获得内容?我想在预定的时间检查网页内容并发送包含该网页部分内容的 toast 通知。这个时候有没有办法动态获取内容?
猜你喜欢
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多