相关疑难解决方法(0)

我在哪里标记lambda表达式异步?

我有这个代码:

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

    var contextMenu = new PopupMenu();

    // Add a command to edit the current Group
    contextMenu.Commands.Add(new UICommand("Edit this Group", (contextMenuCmd) =>
    {
        Frame.Navigate(typeof(LocationGroupCreator), groupName);
    }));

    // Add a command to delete the current Group
    contextMenu.Commands.Add(new UICommand("Delete this Group", (contextMenuCmd) =>
    {
        SQLiteUtils slu = new SQLiteUtils(); …
Run Code Online (Sandbox Code Playgroud)

c# resharper lambda async-await windows-store-apps

199
推荐指数
3
解决办法
11万
查看次数

无法等待异步lambda

想想这个,

Task task = new Task (async () =>{
    await TaskEx.Delay(1000);
});
task.Start();
task.Wait(); 
Run Code Online (Sandbox Code Playgroud)

调用task.Wait()不等待任务完成,下一行立即执行,但如果我将async lambda表达式包装到方法调用中,代码将按预期工作.

private static async Task AwaitableMethod()
{
    await TaskEx.Delay(1000);    
}
Run Code Online (Sandbox Code Playgroud)

然后(根据svick的评论更新)

await AwaitableMethod(); 
Run Code Online (Sandbox Code Playgroud)

c# task-parallel-library async-await async-ctp

62
推荐指数
2
解决办法
6万
查看次数