HttpClient.GetAsync fails in background task with lock screen access and both TimeTrigger or MaintenanceTrigger

前端 未结 1 1004
执笔经年
执笔经年 2021-01-15 03:55

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

I tried using both a TimeTrigger and a MaintenanceTrigger. It ap

相关标签:
1条回答
  • 2021-01-15 04:19

    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)

    Code:

        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();
        }
    
    0 讨论(0)
提交回复
热议问题