调试Task.WhenAny和Push通知 [英] Debugging Task.WhenAny and Push Notifications
问题描述
我有以下代码片段用于处理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:
-
Task.WhenAny(task, Task.Delay(500)) == task
为假.这意味着任务超时 -
Task.WhenAny(task, Task.Delay(500)) == task
为true.然后:- 如果
t1.Status == TaskStatus.RanToCompletion
,则任务运行成功 - 否则,它被取消或出现故障.选中
task.IsFaulted
和task.Exception
以查找更多信息
- 如果
Task.WhenAny(task, Task.Delay(500)) == task
is false. It means that the task timed outTask.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
andtask.Exception
to find more information
- If
如果花费> 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屋!