由于缺少此异步调用的等待而忽略编译器警告是否正确? [英] Am I right to ignore the compiler warning for lacking await for this async call?

查看:21
本文介绍了由于缺少此异步调用的等待而忽略编译器警告是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的 Metro 应用程序的一部分发生异常时,我有以下方法被触发

I have the following method that is triggered when an exception occurs in a part of my Metro application

void Model_ExceptionOccured(Exception ex)
{
    var dlg = new Windows.UI.Popups.MessageDialog("An exception occured during verification: " + ex.Message, "Exception");
    dlg.ShowAsync();
}

'dlg.ShowAsync()'-调用是异步的,但我不想等待结果.编译器会为它生成警告:

The 'dlg.ShowAsync()'-call is asynchronous, but I don't care to wait for the result. The compiler generates a warning for it though:

因为不等待这个调用,所以在调用完成之前会继续执行当前的方法.考虑将await"运算符应用于调用结果.

我应该关心吗?除了消除警告之外,我还有什么理由应该添加 await 关键字?

Should I care? Is there any reason I should add the await keyword, other than to get rid of the warning?

推荐答案

根据以下链接,alexm 给出的答案不正确.在未等待的异步调用期间抛出的异常将丢失.要消除此警告,您应该将异步调用的 Task 返回值分配给一个变量.这确保您可以访问任何抛出的异常,这些异常将在返回值中指明.

According to the below link, the answer given by alexm is not correct. Exceptions thrown during an async call that is not awaited will be lost. To get rid of this warning, you should assign the Task return value of the async call to a variable. This ensures you have access to any exceptions thrown, which will be indicated in the return value.

http://msdn.microsoft.com/en-us/library/hh965065(v=vs.110).aspx (VB.NET)

http://msdn.microsoft.com/en-us/library/hh873131.aspx (C#)

这篇关于由于缺少此异步调用的等待而忽略编译器警告是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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