3

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!

Irelia
  • 76
  • 1
  • 7
  • Possible duplicate of [can not await async lambda](https://stackoverflow.com/questions/13046096/can-not-await-async-lambda) – trailmax Sep 18 '18 at 10:10
  • 2
    Possible duplicate of [Where do I mark a lambda expression async?](https://stackoverflow.com/questions/14015319/where-do-i-mark-a-lambda-expression-async) – Kirk Larkin Sep 18 '18 at 10:11
  • 1
    Stephen Cleary has written an awesome list of delegate types and their await correspondences. https://blog.stephencleary.com/2014/02/synchronous-and-asynchronous-delegate.html – ChristianMurschall Sep 18 '18 at 10:19

3 Answers3

8

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);
}));
Access Denied
  • 8,723
  • 4
  • 42
  • 72
foyss
  • 973
  • 2
  • 8
  • 24
  • 1
    Thanks. This fixed it. I just added async to the last line. Please edit – Irelia Sep 18 '18 at 10:22
  • 2
    Let's keep our fingers crossed, that `UICommand` expects an async delegate. Otherwise it can [wreak havoc](https://blogs.msdn.microsoft.com/pfxteam/2012/02/08/potential-pitfalls-to-avoid-when-passing-around-async-lambdas/). – ChristianMurschall Sep 18 '18 at 10:33
3

In order to mark lambda async use the following syntax:

async (contextMenuCmd) =>
{
   SQLiteUtils rfd = new SQLiteUtils();
   await rfd.DeleteGroupAsync(nameOfGroup ); 
}
Access Denied
  • 8,723
  • 4
  • 42
  • 72
3

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

contextMenu.Commands.Add(new UICommand("Edit this Group", async (contextMenuCmd) =>
{
Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Poul Bak
  • 10,450
  • 5
  • 32
  • 57