C# 后台任务不';开始

C# 后台任务不';开始,c#,windows-runtime,C#,Windows Runtime,我试图弄明白为什么背景任务不会开始,但我不知道我做错了什么 我想做什么:我想有一个自动化的后台任务,它将通过WebApi下载5个最新的项目(数据只有几kB)。下载后,它将检查本地文件,以查看是否有任何新项目可用。如果是这样,我想在LiveTile上创建一个带有新项目数量的徽章 我有以下代码: private BackgroundTaskRegistration ScheduleBackgroundTask() { foreach (var cur in BackgroundTaskReg

我试图弄明白为什么背景任务不会开始,但我不知道我做错了什么

我想做什么:我想有一个自动化的后台任务,它将通过WebApi下载5个最新的项目(数据只有几kB)。下载后,它将检查本地文件,以查看是否有任何新项目可用。如果是这样,我想在LiveTile上创建一个带有新项目数量的徽章

我有以下代码:

private BackgroundTaskRegistration ScheduleBackgroundTask()
{
    foreach (var cur in BackgroundTaskRegistration.AllTasks)
    {
        if (cur.Value.Name == "TimeTriggeredTask")
        {
            return (BackgroundTaskRegistration)(cur.Value);
        }
    }

    var builder = new BackgroundTaskBuilder();

    builder.Name = "TimeTriggeredTask";
    builder.TaskEntryPoint = "Tasks.UpdateItemTask";
    builder.SetTrigger(new MaintenanceTrigger(15, false));
    builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
    builder.AddCondition(new SystemCondition(SystemConditionType.UserNotPresent));

    BackgroundTaskRegistration task = builder.Register();
    return task;
}
我的工作是这样的:

namespace Tasks
{
    public sealed class UpdateItemTask : IBackgroundTask
    {
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Starting");

            BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();

            DataHandler dataHandler = new DataHandler();
            BindableCollection<GeekAndPokeItemViewModel> latestItemsOnline = await dataHandler.GetData("10");
            BindableCollection<GeekAndPokeItemViewModel> latestItemsLocal = await dataHandler.GetLocalData();

            int difference = latestItemsOnline.Except(latestItemsLocal).Count();

            if (difference > 0)
            {
                BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();
                BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent((uint)difference);

                // send the notification to the app's application tile
                BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
            }

            _deferral.Complete();
        }
    }
}
命名空间任务
{
公共密封类UpdateItemTask:IBackgroundTask
{
公共异步无效运行(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine(“启动”);
BackgroundTaskDeleral _deleral=taskInstance.getDeleral();
DataHandler DataHandler=新DataHandler();
BindableCollection latestItemsOnline=等待dataHandler.GetData(“10”);
BindableCollection latestItemsLocal=await dataHandler.GetLocalData();
int difference=latestItemsOnline.Except(latestItemsLocal.Count();
如果(差异>0)
{
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();
BadgeNumericNotificationContent badgeContent=新的BadgeNumericNotificationContent((uint)差异);
//将通知发送到应用程序的应用程序磁贴
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
}
_延迟。完成();
}
}
}
在我的appmanifest中,backgroundTask使用任务“timer”和正确的入口点进行扩展

所有代码都在1个项目中

即使附加了调试器(在不启动应用程序的情况下调试程序,并强制启动任务),它也不会命中我的任务(或断点),并在eventviewer中给出以下结果:

具有入口点Tasks.UpdateItemTask和名称TimeTriggeredTask的后台任务无法激活,错误代码为0x80010008


我一直在检查,但即使是那些也没有帮助。我想说这并不难,但我不能让它工作。

我终于让它工作了

第一:我已将任务作为Windows RT组件类型移动到新程序集中,并在我的WinRT应用程序中添加了一个引用


第二:我把BackgroundTaskRegistration和UpdateItemTask放在一个cs文件中,只有任务是密封的。我需要将背景任务注册也设置为密封,以便编译。一旦我强制触发触发器,它最终会到达断点…

可能只是在您的任务中显示一条日志消息,看看它是否就是找不到它。调试器运行时没有出现问题,只是它在任务开始时没有到达断点,这意味着任务甚至没有被触发。。。我想说,登录任务不会有帮助,因为任务根本没有执行……我刚刚读了一篇关于维护触发器类别的文章。这个触发器实际上是一个SystemTrigger,而不是TimeTrigger(在我的appmanifest中定义)。我改变了这个,但仍然没有结果。我仍然有相同的错误消息…我终于让它工作了!!第一:我已将任务作为Windows RT组件类型移动到新程序集中,并在我的WinRT应用程序中添加了一个引用。第二:我把BackgroundTaskRegistration和UpdateItemTask放在一个cs文件中,只有任务是密封的。我需要将背景任务注册也设置为密封,以便编译。一旦我强制触发,它最终会到达断点…只看到了你的帖子,但有个好消息:)