【问题标题】:ContinueWith not waiting for task to completeContinueWith 不等待任务完成
【发布时间】:2016-11-30 02:11:07
【问题描述】:

我有一个从 API 检索数据的函数(如下)。如果我在反序列化它的行设置断点,那么我可以看到它填充了很棒的数据。

当我继续时,它进入第二个函数(如下)并引发错误。错误旁边写着Not yet computed,因此抛出异常。

当我使用一个小列表时,它工作得很好(我认为它是一小组数据)。

当我使用ContinueWith(等待任务完成)时,这怎么可能?

    public static async Task<Data> GetAllCardsInSet(string setName)
    {
                setName = WebUtility.UrlEncode(setName);
                var correctUri = Path.Combine(ApiConstants.YugiohGetAllCardsInSet, setName);
                Console.WriteLine();
                using (var httpClient = new HttpClient())
                {
                    var response =
                        await httpClient.GetAsync(correctUri);
                    var result = await response.Content.ReadAsStringAsync();
                    var cardData = JsonConvert.DeserializeObject<CardSetCards>(result);
                    for (int i = 0; i < cardData.Data.Cards.Count; i++)
                    {
                        cardData.Data.Cards[i] = FormatWords(cardData.Data.Cards[i]);
                    }
                    return cardData.Data;
                }
    }


    private void GetYugiohCardsAndNavigate(string name)
    {
    var cardSetData = YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name).ContinueWith((result) =>
                {
                    //var cards = await YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name);
                    try
                    {
                        this.mainPage.NavigateToYugiohCardListPage(result.Result);
                    }
                    catch (Exception e)
                    {
                        HelperFunctions.ShowToastNotification("Trading Card App", "Sorry, we could not fetch this set");
                    }

                });
}

【问题讨论】:

  • 如果你避免ContinueWith只是等待GetAllCardsInSet方法会发生什么?
  • 给我一个空引用异常
  • 你能调查一下如果你返回await Task.FromResult(cardData.Data)而不是cardData.Data会发生什么。
  • 您的GetAllCardsInSet 正在引发异常。检查 ContinueWith 块中的IsFaulted result 参数,查看GetAllCardsInSet 是否抛出异常
  • @magna_nz:永远不要使用ContinueWith。请改用await

标签: c# .net asynchronous async-await


【解决方案1】:

您的GetAllCardsInSet 方法无需更改。
但是这种方法的使用是可以重构的。
方法GetAllCardsInSet返回Task而你没有观察到这个Task的完成。
您需要检查Task 是否成功完成,这是使用await 关键字的最简单方法。如果任务以异常完成,等待任务将解包返回值或抛出异常。

对于在GetYugiohCardsAndNavigate 中使用async/await 将方法签名更改为异步并返回Task

private async Task GetYugiohCardsAndNavigate(string name)
{
    try
    {
        var cardSetData  = await YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name);
        this.mainPage.NavigateToYugiohCardListPage(cardSetData);
    }
    catch (Exception e)
    {
        HelperFunctions.ShowToastNotification("Trading Card App", 
                                              "Sorry, we could not fetch this set");
    }
}

【讨论】:

    【解决方案2】:

    您在没有Wait 的同步方法中调用了异步方法。应该是这样的:

                YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name).ContinueWith((result) =>
                {
                    //var cards = await YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name);
                    try
                    {
                        this.mainPage.NavigateToYugiohCardListPage(result.Result);
                    }
                    catch (Exception e)
                    {
                        HelperFunctions.ShowToastNotification("Trading Card App", "Sorry, we could not fetch this set");
                    }
    
                }).Wait();
    

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 1970-01-01
      • 2015-11-16
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 2021-01-26
      相关资源
      最近更新 更多