Search code examples
c#async-awaitwarnings

Compiler warning 'await' operator, should I ignore it?


I have a little button code like this:

private void CreateButton_Click(object sender, RoutedEventArgs e)
{
    var MsgDialog = new MessageDialog("MY MESSAGE");
    MsgDialog.Commands.Add(new UICommand("OK", (UICommandInvokedHandler) =>
        {
            // IF USER PRESSES OK, CHECKBOX WILL GET CHECKED
            DontDisplayCheckBox.IsChecked = true;
        }));

       // DISPLAY MESSAGE IF TEXTBOX IS EMPTY
    if (TileName.Text == "")
        MsgDialog.ShowAsync();
    else
        // REST OF THE CODE;
}

The code works but it keeps giving me a warning:

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/could I just ignore it? If not, how to I implement the 'await' operator in this case?


Solution

  • add async to the method defintion

    private async void CreateButton_Click(object sender, RoutedEventArgs e)
    

    and add await to

    await MsgDialog.ShowAsync();