【问题标题】:ContinueWith not being called asyncContinueWith 不被称为异步
【发布时间】:2016-08-11 08:54:04
【问题描述】:

我正在尝试在执行语句的下一行之前 httpget 一些值。我需要等待此调用返回,以便我可以使用我反序列化到列表中的值。

由于我希望异步调用首先完成,因此我将其包装在 Task 中。它起作用了,并且成功地检索了 JSON。然后我无法让它进入ContinueWith 块。为什么即使任务完成了它也不进去(?)。

我怎么称呼它:

Task f = Task.Run(() =>
            {
                var task = RetrieveDataAsync();
            }).ContinueWith((antecedent) =>
            {
                pokemonListActivityListView.Adapter = new PokemonListAdapter(this, pokemonList);
                pokemonListActivityListView.FastScrollEnabled = true;
                pokemonListActivityListView.ItemClick += PokemonListActivityListViewOnItemClick;
            });

RetrieveDataAsync 方法:

private async Task RetrieveDataAsync()
        {
            string dataUri = "http://pokemonapp6359.azurewebsites.net/Pkmn/GetAllPokemon";
            using (var httpClient = new HttpClient())
            {
                var uri = new Uri(string.Format(dataUri, string.Empty));


                //DisplayProgressBar(BeforeOrAfterLoadState.Before, progressBarView);
                var response = await httpClient.GetAsync(uri);
                //DisplayProgressBar(BeforeOrAfterLoadState.After, progressBarView);

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    pokemonList = JsonConvert.DeserializeObject<List<PokemonDTO>>(content);
                    //canPressButtons = true; //fix this when implement local db

                    Utilities.Utilities.ShowToast(this, "Successfully fetched data", ToastLength.Short, GravityFlags.Center);
                    return;
                }
                else
                {
                    Utilities.Utilities.ShowToast(this, "Failed to fetch data", ToastLength.Short, GravityFlags.Center);
                    return;
                }

            }
        }

当我得到 JSON 后,为什么我的代码没有进入 ContinueWith ?谢谢!

【问题讨论】:

    标签: c# android .net asynchronous


    【解决方案1】:

    您不必等待它完成,而不仅仅是分配热门任务。您必须在该任务上致电 ContinueWith

    var task = RetrieveDataAsync();
    task.ContinueWith( ... );
    

    或者等待任务:

    var result = await RetrieveDataAsync();
    
    ... // continue
    

    【讨论】:

      【解决方案2】:

      问题是您忽略了从RetrieveDataAsync 返回的任务。如果您从您的 lambda 表达式中返回该任务,那么它将按照您的预期运行。

      附带说明,您不应该使用ContinueWith;这是一个危险的 API。使用await 而不是ContinueWith

      await Task.Run(() => RetrieveDataAsync());
      pokemonListActivityListView.Adapter = new PokemonListAdapter(this, pokemonList);
      pokemonListActivityListView.FastScrollEnabled = true;
      pokemonListActivityListView.ItemClick += PokemonListActivityListViewOnItemClick;
      

      【讨论】:

      • 任何进一步的信息为什么 ContinueWith 是危险的?谢谢
      • @leon22:problems with ContinueWithStartNew's problems 相同:不了解async,令人惊讶的默认调度程序行为,并且有一个简单的内置替换(await)。
      猜你喜欢
      • 1970-01-01
      • 2014-02-21
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      相关资源
      最近更新 更多