Am I correct to ignore the compiler warning due to the lack of waiting for this asynchronous call?

I have the following method that fires 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 ()" is an asynchronous call, but I do not want to wait for the result. The compiler generates a warning for it:

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.

I do not care? Is there a reason why I should add a wait keyword other than getting rid of the warning?

+30
c # windows-store-apps windows-runtime async-await
Jun 21 2018-12-21T00:
source share
3 answers

According to the link below, the answer given by alexm is incorrect. Exceptions that occur during an asynchronous call that are not expected will be lost. To get rid of this warning, you must assign the return value of the asynchronous call task to a variable. This ensures that you get access to any exceptions that are 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 #)

+27
Aug 27 '12 at 15:36
source share

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

What really happens depends on the .NET exception policy.

This article on MSDN mentions the following:

If you do not expect a task that throws an exception, or access to the Exception property, the exception is redistributed according to the .NET exception policy when the task collects garbage.

When VS 2012 was submitted, the default policy for unhandled task exceptions changed from the completion process to ignore the exception.

+6
Jun 21 2018-12-21T00:
source share

I ran into the same problem and here is my solution:

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

Here is a quick example: (in the 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; } 
-one
Jul 26 '13 at 20:29
source share



All Articles