最佳答案
如果您可以使用 C# 7 功能,请将其分配给“丢弃”变量:
_ = iPushNotificationService.SendPushNotification(objNotificationMessage);
如果您不能使用 C# 7 - 您可以将它分配给某个变量:
var t = iPushNotificationService.SendPushNotification(objNotificationMessage);
或者使用这样的扩展方法:
public static class TaskExtensions {
public static void Ignore(this Task t) {
}
}
然后:
iPushNotificationService.SendPushNotification(objNotificationMessage).Ignore();
请注意,您可能对结果不感兴趣,但如果它未能发送该通知(至少记录该通知),您可能会感兴趣。在这种情况下,您可以使用 ContinueWith
:
SendNotification().ContinueWith(c =>
{
// do something with c.Exception
}, TaskContinuationOptions.OnlyOnFaulted).Ignore();
或者将其移动到单独的异步方法中:
static async void SendNotification() { // if you hate async void - use Task
try {
await iPushNotificationService.SendPushNotification(objNotificationMessage);
}
catch (Exception ex {
// log it
}
}
然后调用它。
关于c# - Async/Await 方法 - 无需等待方法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49047012/