后台任务无效

时间:2017-05-27 10:08:36

标签: c# uwp background-process

我不确定之前是否曾经问过这个问题,但是我搜索了谷歌,但没有提出这个问题。我有2个提交到商店的应用程序,它们都有几乎相同的后台任务,特别是每个任务2个。

我注册了后台任务,当应用程序启动时,如果应用程序得到更新,我会检查应用程序版本并按照Microsoft文档中针对进程外后台任务的UWP应用程序中所述做适当的事情。

后台任务工作正常,但是到目前为止,我没有看到任何关于我系统中后台任务的活动。我检查了事件查看器,我看到根据应用程序中定义的触发器,后台任务“尝试启动”但是当任务被异常杀死并且从未实际运行时系统存在一些问题。

请注意,虽然调试我可以毫无问题地运行后台任务,但是当它们自己启动时,任务就不会运行。

以下是事件查看器最新日志的屏幕截图:

WFW电影&amp ;;的任务1电视 Task 1 for App - WFW Movies & TV

WFW电影&amp ;;的任务2电视 Task 2 for App - WFW Movies & TV

Anime Deed的任务1 Task 1 for App - Anime Deed

动漫契约任务2 Task 2 for App - Anime Deed

我用于注册任务的代码:

private static async void BackgroundTasksRegistrationProcedure()
    {
        string curVer = DataHelper.GetAppVersion();
        TimeTrigger timeTrigger = new TimeTrigger(15, false);
        SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
        bool doRegister = false;
        var status1 = await BackgroundExecutionManager.RequestAccessAsync();
        switch (status1)
        {
            case BackgroundAccessStatus.DeniedByUser:
                {
                    doRegister = false;
                    break;
                }

            case BackgroundAccessStatus.AllowedSubjectToSystemPolicy:
                {
                    doRegister = true;
                    break;
                }

            case BackgroundAccessStatus.AlwaysAllowed:
                {
                    doRegister = true;
                    break;
                }

            default:
                break;
        }
        var versionKey = "app_version";
        if (!DataHelper.LocalDataContainer.Values.ContainsKey(versionKey))
        {
            DataHelper.LocalDataContainer.Values[versionKey] = curVer;
            // run tasks registration here
            if (doRegister)
            {
                RegisterBackgroundTask(EpisodeUpdateTaskEntryPoint, EpisodeUpdateTaskName, new TimeTrigger(120, false), internetCondition);
                RegisterBackgroundTask(TileUpdateTaskEntryPoint, TileUpdateTaskName, timeTrigger, internetCondition);
            }
        }
        else
        {
            // compare app version
            if (DataHelper.LocalDataContainer.Values[versionKey].ToString() == curVer)
            {
                // run tasks registration here
                if (doRegister)
                {
                    RegisterBackgroundTask(EpisodeUpdateTaskEntryPoint, EpisodeUpdateTaskName, new TimeTrigger(120, false), internetCondition);
                    RegisterBackgroundTask(TileUpdateTaskEntryPoint, TileUpdateTaskName, timeTrigger, internetCondition);
                }
            }
            else
            {
                DataHelper.LocalDataContainer.Values[versionKey] = curVer;
                // remove task registrations
                BackgroundExecutionManager.RemoveAccess();
                // run tasks registrations
                await BackgroundExecutionManager.RequestAccessAsync();
                if (doRegister)
                {
                    RegisterBackgroundTask(EpisodeUpdateTaskEntryPoint, EpisodeUpdateTaskName, new TimeTrigger(120, false), internetCondition);
                    RegisterBackgroundTask(TileUpdateTaskEntryPoint, TileUpdateTaskName, timeTrigger, internetCondition);
                }
            }
        }
    }

    public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint, string name, IBackgroundTrigger trigger, IBackgroundCondition condition)
    {
        // We'll add code to this function in subsequent steps.
        foreach (var cur in BackgroundTaskRegistration.AllTasks)
        {
            if (cur.Value.Name == name)
            {
                //
                // The task is already registered.
                //
                return (BackgroundTaskRegistration)(cur.Value);
            }
        }
        var builder = new BackgroundTaskBuilder()
        {
            Name = name
        };
        // in-process background tasks don't set TaskEntryPoint
        if (taskEntryPoint != null && taskEntryPoint != string.Empty)
        {
            builder.TaskEntryPoint = taskEntryPoint;
        }
        builder.SetTrigger(trigger);
        if (condition != null)
        {
            builder.AddCondition(condition);
        }
        BackgroundTaskRegistration task = builder.Register();
        return task;
    }

请帮助我理解为什么会这样。我可以成功调试任务,但是当应用程序未运行时,它们不会自行运行。感谢

更新

我发现了与BackgroundTaskHost.exe

相关的其他错误
Faulting application name: backgroundTaskHost.exe, version: 10.0.15063.0, time stamp: 0x9ad06afb
Faulting module name: CoreCLR.dll, version: 4.6.25129.1, time stamp: 0x58dc3e7d
Exception code: 0xc0000005
Fault offset: 0x0014101d
Faulting process ID: 0x18bc
Faulting application start time: 0x01d2d953c94d6e41
Faulting application path: C:\Windows\syswow64\backgroundTaskHost.exe
Faulting module path: C:\Program Files\WindowsApps\Microsoft.NET.CoreRuntime.1.1_1.1.25129.1_x86__8wekyb3d8bbwe\CoreCLR.dll
Report ID: 8a3a3035-d95b-49bb-a106-d645c604c39b
Faulting package full name: 51918AhmarAftab.WFWMoviesTV_1.3.442.0_x86__rm40d0p5knk1c
Faulting package-relative application ID: App

我认为这是一个Windows问题,考虑到错误在.Net.CoreRuntime

0 个答案:

没有答案