【问题标题】:Wait for WCF async method to complete before continue在继续之前等待 WCF 异步方法完成
【发布时间】:2015-07-25 05:38:05
【问题描述】:

在继续应用程序之前,我已经尝试了几个小时来等待异步方法完成。如果我调试一切正常,因为我实际上在等待异步方法完成,但没有调试代码在调用方法时执行,但没有任何效果。

这是我的代码:

private async void MenuPage_Loaded(object sender, RoutedEventArgs e)
        {
            MenuItems = await SetItemSource();
            ItemSource = new ObservableCollection<AlphaKeyGroup<Menu>>((AlphaKeyGroup<Menu>.CreateGroups(MenuItems, CultureInfo.CurrentUICulture, s => s.MenuName, true)));
            ((CollectionViewSource)Resources["MenuGroups"]).Source = ItemSource;
        }

        private async Task<ObservableCollection<Menu>> SetItemSource()
        {
            return await MyWinService.GetMenuEntriesAsync();

        }

【问题讨论】:

  • 我这样做了,但仍然失败:
  • 私有任务> SetItemSource() { return MyWinService.GetMenuEntriesAsync(); }

标签: c# wcf asynchronous win-universal-app


【解决方案1】:

移除 SetItemSource 方法中的 async/await。您正在返回一个等待的任务,告诉 MenuPage_Loaded 在该点继续。 第二种方法不需要异步,因为没有延续。

【讨论】:

    【解决方案2】:

    好的,我这样做了:

    private async void MenuPage_Loaded(object sender, RoutedEventArgs e)
            {
                var MenuItemsTask = MyWinService.GetMenuEntriesAsync();
                MenuItems = await MenuItemsTask;
                ItemSource = new ObservableCollection<AlphaKeyGroup<Menu>>((AlphaKeyGroup<Menu>.CreateGroups(MenuItems, CultureInfo.CurrentUICulture, s => s.MenuName, true)));
                ((CollectionViewSource)Resources["MenuGroups"]).Source = ItemSource;
            }
    

    我发誓它不起作用,但我重新发布了我的 ServiceReference(没有更改任何内容),突然它起作用了,我的意思是,我不知道发生了什么,我不想不知道发生了什么哈哈。

    感谢您的帮助。

    【讨论】:

    • 没关系,它开始工作了,现在又失败了。
    • 好的,我想我知道发生了什么,有时我会收到一条“您的服务已在时钟旁边托管消息”,当这种情况发生时一切正常,但有时它没有托管,为什么?跨度>
    猜你喜欢
    • 2016-09-23
    • 2011-02-15
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多