Am I right to ignore the compiler warning for lack

2019-01-15 11:40发布

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();
}

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

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

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

3条回答
2楼-- · 2019-01-15 12:25

The issue with that is if the code in dlg.ShowAsync(); throws an exception it will be left unhandled and will be re-thrown later by the Finalizer thread potentially causing your program termination.

What happens in reality depends on .NET exception policy

This article on MSDN mentions this:

If you do not wait on a task that propagates an exception, or access its Exception property, the exception is escalated according to the .NET exception policy when the task is garbage-collected.

When VS 2012 was eventually shipped, the default policy for unhandled task exceptions changed from terminating process to ignore exception.

查看更多
三岁会撩人
3楼-- · 2019-01-15 12:31

I ran into the same problem, and here's my solution:

I created a Task object, assigned the output of the async function to the Task object, and used a Timer to periodically check the status of the task.

Here's a brief example: (in my Update_Click event handler)

StatusLabel.Text = "Preparing " + feedArticleList1.Feed.Title;
UpdateCheck.Enabled = true;
UpdateTask = feedArticleList1.Feed.UpdateFeedAsync();

Later, in the event handler for my timer, I check UpdateTask.Status:

switch (UpdateTask.Status)
{
    case TaskStatus.Canceled:
    case TaskStatus.Created:
    case TaskStatus.Running:
    case TaskStatus.WaitingForActivation:
    case TaskStatus.WaitingForChildrenToComplete:
    case TaskStatus.WaitingToRun:
        StatusLabel.Text = UpdateTask.Status.ToString();
        break;
    case TaskStatus.RanToCompletion:
        StatusLabel.Text = "Update Complete " + DateTime.Now.ToShortTimeString();
        UpdateCheck.Enabled = false;
        break;
    case TaskStatus.Faulted:
        throw (UpdateTask.Exception);
    default:
        break;
}
查看更多
地球回转人心会变
4楼-- · 2019-01-15 12:37

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#)

查看更多
登录 后发表回答