调试Task.WhenAny和Push通知 [英] Debugging Task.WhenAny and Push Notifications

查看:86
本文介绍了调试Task.WhenAny和Push通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段用于处理Azure Notification Hub推送通知:

I have the following snippet to handle Azure Notification Hub push notifications:

var alert = "{\"aps\":{\"alert\":\"" + message + "\"}}";

var task = AzurePushNotifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, username);

if (await Task.WhenAny(task, Task.Delay(500)) == task)
{
     success = true;
}

有时候,这会失败-我正试图找出原因?

Occasionally, this will fail - I'm trying to figure out why ?

使用Task.WhenAny运行事物时获取诊断信息的最佳方法是什么?

What's the best way to get some diagnostic information when running things with Task.WhenAny?

我想知道是否引发了异常,或者是否发生了超时.

I'd like to know if either an exception was thrown, or if the timeout has been hit.

推荐答案

您基本上有三种可能性:

You basically have three possibilities:

  1. Task.WhenAny(task, Task.Delay(500)) == task为假.这意味着任务超时
  2. Task.WhenAny(task, Task.Delay(500)) == task为true.然后:
    • 如果t1.Status == TaskStatus.RanToCompletion,则任务运行成功
    • 否则,它被取消或出现故障.选中task.IsFaultedtask.Exception以查找更多信息
  1. Task.WhenAny(task, Task.Delay(500)) == task is false. It means that the task timed out
  2. Task.WhenAny(task, Task.Delay(500)) == task is true. Then either:
    • If t1.Status == TaskStatus.RanToCompletion, then the task ran successfully
    • Otherwise, it's either cancelled or faulted. Check task.IsFaulted and task.Exception to find more information

如果花费> 500毫秒,我希望它失败,但是我想知道这就是失败的原因

If it takes > 500ms, I want it to fail, but I want to know that's why it failed

在这种情况下,您唯一可以知道的是通知超时.由于任务尚未完成,因此没有日志可记录.如果要在最终完成时检查其状态,可以链接一个延续:

In that case, the only thing you can know is that the notification timed out. There is no exception to log since the task hasn't completed yet. If you want to check the status when it eventually completes, you can chain a continuation:

task.ContinueWith(t => 
{
    // Log t.Exception
}, TaskContinuationOptions.OnlyOnFaulted);

这篇关于调试Task.WhenAny和Push通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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