繁体   English   中英

在没有等待任务完成的情况下

[英]ContinueWith not waiting for task to complete

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

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

当我用一个小的列表来做时,它就可以正常工作(我假设它的cos是一小部分数据)。

当我使用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");
                    }

                });
}

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

对于在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");
    }
}

您在没有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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM