HttpClient.GetAsync在具有锁定屏幕访问权限和TimeTrigger或MaintenanceTrigger的后台任务中失败 [英] HttpClient.GetAsync fails in background task with lock screen access and both TimeTrigger or MaintenanceTrigger

查看:81
本文介绍了HttpClient.GetAsync在具有锁定屏幕访问权限和TimeTrigger或MaintenanceTrigger的后台任务中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows 8 Metro应用程序的后台任务中运行时,Client.GetAsync对于我来说似乎失败.

Client.GetAsync appears to fail for me when running in a background task in my Windows 8 Metro app.

我尝试同时使用TimeTrigger和MaintenanceTrigger.看来也不例外.调试它时,即使有更多方法可以执行以及围绕它的try-catch,它也只在该行退出(如果我一直按一步的话).

I tried using both a TimeTrigger and a MaintenanceTrigger. It appears that there is no exception. When debugging it, it just exits at that line (if I keep pressing step over), even though there is way more to execute as well as a try-catch around it.

这使我相信Windows正在取消我的任务.所以我没有运气就听了取消的活动.

This leads me to believe that Windows is cancelling my task. So I listened to the cancelled event with no luck.

我已宣布可以访问Internet,并且该任务已正确注册.此外,我的应用程序具有完全锁定屏幕访问权限.

I have declared Internet access as a capability, and the task has been properly registered. Furthermore, my app has full lock screen access.

我在做什么错了?

这是我当前用于注册后台任务的代码.

Here's the code I use to register the background task currently.

if (await ObtainLockScreenAccess())
{
    const string name = "Task";

    var updaterTasks = BackgroundTaskRegistration.AllTasks.Where(t => t.Value.Name == name);
    if (!updaterTasks.Any())
    {

        var builder = new BackgroundTaskBuilder();
        builder.Name = name;
        builder.TaskEntryPoint = "Stackstabilizer.BackgroundTasks.UpdateBackgroundTask";

        var trigger = new MaintenanceTrigger(15, false);
        var condition = new SystemCondition(SystemConditionType.InternetAvailable);

        builder.AddCondition(condition);
        builder.SetTrigger(trigger);

        var task = builder.Register();

        Debug.WriteLine("Task has been registered");

    }
}

编辑:这是后台任务签名:

Edit Here's the background task signature:

namespace Stackstabilizer.BackgroundTasks
{
    // You must use a sealed class, and make sure the output is a WINMD. 
    public sealed class UpdateBackgroundTask : IBackgroundTask
    {
        public async void Run(IBackgroundTaskInstance taskInstance)
        {

            var deferral = taskInstance.GetDeferral();

            try { ... } finally { deferral.Complete(); }

        }
     }

}

这是我的声明的详细信息.

And here's some details on my declarations.

推荐答案

我以前遇到过与您类似的问题,而解决问题的方法是在异步任务中进行另一个延迟.我的想法是,每当您执行异步任务时,都需要进行延迟. (我不确定这是否是最佳做法,但嘿,它能奏效)

I had a similar problem to you before, and the way I solved it was to have another deferral in the async task. I think the idea is that, whenever you have an async task, you need a deferral. (I'm not sure if it's best practice, but hey, it works)

代码:

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        updateAllTiles(taskInstance);
    }


    public async void updateAllTiles(IBackgroundTaskInstance taskInstance)
    {
        // Need to use a deferral to run async tasks in the background task
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        dosomething(taskInstance);

        deferral.Complete();
    }

    private async void dosomething(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        HttpClient client = new HttpClient();
        HttpResponseMessage resp = await client.GetAsync(new Uri(url));

        updateTile(resp);

        deferral.Complete();
    }

这篇关于HttpClient.GetAsync在具有锁定屏幕访问权限和TimeTrigger或MaintenanceTrigger的后台任务中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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