Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I right to ignore the compiler warning for lacking await for this async call?

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?

like image 448
Nilzor Avatar asked Jun 21 '12 21:06

Nilzor


People also ask

Is it OK to not await async?

Yes. If you don't need to wait, don't wait.

What happens if you dont await an async method?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Can you call an async method without await?

You can call this method with or without the await keyword. The syntax with the await keyword looks like this: Customer cust = await GetCustomerById("A123");

When an asynchronous method is executed the code runs but nothing happens other than compiler warning?

When a asynchronous method is executed, the code runs but nothing happens other than a compiler warning. What is most likely causing the method to not return anything? (A) The return yield statement is missing at the end of the method.


2 Answers

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

like image 109
Ryan Horath Avatar answered Sep 29 '22 00:09

Ryan Horath


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.

like image 41
alexm Avatar answered Sep 29 '22 00:09

alexm