Show/Hide Toolbars

XSharp

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.

 

The current method calls an async method that returns a Task or a Task<TResult> and doesn’t apply the await operator to the result. 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 what you expect. Usually other aspects of the calling method depend on the results of the call or, minimally, the called method is expected to complete before you return from the method that contains the call.

 

An equally important issue is what happens to exceptions that are raised in the called async method. An exception that's raised in a method that returns a Task or Task<TResult> is stored in the returned task. If you don't await the task or explicitly check for exceptions, the exception is lost. If you await the task, its exception is rethrown.

 

As a best practice, you should always await the call.

 

You should consider suppressing the warning only if you're sure that you don't want to wait for the asynchronous call to complete and that the called method won't raise any exceptions. In that case, you can suppress the warning by assigning the task result of the call to a variable.

 

The following example shows how to cause the warning, how to suppress it, and how to await the call.

 

 

 

 

 

In the example, if you choose Call #1 or Call #2, the unawaited async method (CalledMethodAsync) finishes after both its caller (CallingMethodAsync) and the caller's caller (startButton_Click) are complete. The last line in the following output shows you when the called method finishes. Entry to and exit from the event handler that calls CallingMethodAsync in the full example are marked in the output.

 

 

 

 

 

You

can also suppress compiler warnings by using #pragma warning directives.

Example

 

The following Windows Presentation Foundation (WPF) application contains the methods from the previous example. The following steps set up the application.

 

1.Create a WPF application, and name it AsyncWarning.

2.In the Visual Studio Code Editor, choose the MainWindow.xaml tab.

    If the tab isn't visible, open the shortcut menu for MainWindow.xaml in Solution Explorer, and then choose View Code.

3.Replace the code in the XAML view of MainWindow.xaml with the following code.

 

 

 

 

 

A simple window that contains a button and a text box appears in the Design view of MainWindow.xaml.

 

For more information about the XAML Designer, see Creating a UI by using XAML Designer. For information about how to build your own simple UI, see the "To create a WPF application" and "To design a simple WPF MainWindow" sections of Walkthrough: Accessing the Web by Using Async and Await.

 

4.   Replace the code in MainWindow.xaml.prg with the following code.

 

 

 

 

 

5.  Choose the F5 key to run the program, and then choose the Start button.

 

    The expected output appears at the end of the code.