Irelia
Irelia

Reputation: 76

How do I mark a async lambda expression?

I have some code here and wish to know where to place an await. I have tried lamba => and the normal method but have come to no success.

private async void ContextMenuAbroad(object sender, RightTappedRoutedEventArgs args)
{
    CheckBox ckbx = null;
    if (sender is CheckBox)
    {
        ckbx = sender as CheckBox;
    }
    if (null == ckbx)
    {
        return;
    }
    string nameOfGroup = ckbx.Content.ToString();

    var contextMenu = new PopupMenu();

    contextMenu.Commands.Add(new UICommand("Edit this Group", (contextMenuCmd) =>
    {
        Frame.Navigate(typeof(LocationGroupCreator), nameOfGroup );
    }));

    contextMenu.Commands.Add(new UICommand("Delete this Group", (contextMenuCmd) =>
    {
        SQLiteUtils rfd = new SQLiteUtils();
        rfd.DeleteGroupAsync(nameOfGroup ); 
    }));

    await contextMenu.ShowAsync(args.GetPosition(this));
}

I added an await, however I require to add an async somewhere... but where?

Resharpers inspection complained about: "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"

Any help is greatly appreciated!

Upvotes: 3

Views: 2605

Answers (3)

foyss
foyss

Reputation: 983

Simply prepend async before the argument list

// Command to delete the current Group
contextMenu.Commands.Add(new UICommand("Delete this Group", async (contextMenuCmd) =>
{
    SQLiteUtils rfd = new SQLiteUtils();
    await rfd.DeleteGroupAsync(groupName);
}));

Upvotes: 8

Poul Bak
Poul Bak

Reputation: 10929

Simply add it in front of the parentheses, like this:

contextMenu.Commands.Add(new UICommand("Edit this Group", async (contextMenuCmd) =>
{

Upvotes: 3

Access Denied
Access Denied

Reputation: 9461

In order to mark lambda async use the following syntax:

async (contextMenuCmd) =>
{
   SQLiteUtils rfd = new SQLiteUtils();
   await rfd.DeleteGroupAsync(nameOfGroup ); 
}

Upvotes: 3

Related Questions