0

Riot Games API からデータを取得してユーザーに表示する Xamarin を使用して Android アプリを構築しています。HTTP リクエストをバックグラウンドで実行し、完了したら UI を更新したいと考えています。ThreadPool.QueueUserWorkItem() を使用してみましたが、すぐに以下のコードが実行され、データが取得されるまで待機する必要があります。これは、私のポータブル クラス ライブラリのコードです。

 public void GetSummonerInformation()
        {
            try
            {
                    HttpResponseMessage response = httpClient.GetAsync(url).Result;
                    response.EnsureSuccessStatusCode();
                    string result = response.Content.ReadAsStringAsync().Result;

                    var data = JsonConvert.DeserializeObject<Dictionary<string, App2.Models.SummonerDto>>(result);

                    var name1 = data.First().Value.name;
                    var id = data.First().Value.id;
                    var profileIconId1 = data.First().Value.profileIconId;
                    var revisionDate1 = data.First().Value.revisionDate;
                    sumId = id;
                    sumName1 = name1;
                    sumProfileIconId = profileIconId1;
                    sumRevisionDate = revisionDate1;
                    System.Diagnostics.Debug.WriteLine("{0} this is the  {1}", data.First().Value.name, data.First().Value.profileIconId);

            }catch(Exception ex)
            { System.Diagnostics.Debug.WriteLine(ex.Message); }

これは私のAndroid mainactivity.csのコードです

button2nd.Click += delegate
           {
                   //Runs the http request on a background thread so the application doesnt hang.Need to make the code after that to wait for the request to end and then exexute
                   //because it gives me a blank icon and i need to press the button again.
               ThreadPool.QueueUserWorkItem(o => mclass.GetSummonerInformation());
               System.Diagnostics.Debug.WriteLine(MyClass.sumProfileIconId);
               iconUrl = string.Format("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/profileicon/" + MyClass.sumProfileIconId + ".png");
               DisplaySummonerIcon();
               DisplaySummonerInformation();
}

ありがとう!

4

2 に答える 2

0

変化する:

ThreadPool.QueueUserWorkItem(o => mclass.GetSummonerInformation());

と:

await Task.Run(() => mclass.GetSummonerInformation());

「delegate」の前に「async」キーワードを追加します。

于 2016-12-14T20:37:45.060 に答える
0

非同期 API ( HttpClient) を使用しているため、以下を使用する必要がありますawait

public async Task GetSummonerInformationAsync()
{
  try
  {
    HttpResponseMessage response = await httpClient.GetAsync(url);
    response.EnsureSuccessStatusCode();
    string result = await response.Content.ReadAsStringAsync();
    var data = JsonConvert.DeserializeObject<Dictionary<string, App2.Models.SummonerDto>>(result);
    ...
    System.Diagnostics.Debug.WriteLine("{0} this is the  {1}", data.First().Value.name, data.First().Value.profileIconId);
  }
  catch(Exception ex)
  {
    System.Diagnostics.Debug.WriteLine(ex.Message);
  }
}

次のように呼び出されます。

button2nd.Click += async (_, __) =>
{
  await mclass.GetSummonerInformationAsync();
  System.Diagnostics.Debug.WriteLine(MyClass.sumProfileIconId);
  iconUrl = string.Format("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/profileicon/" + MyClass.sumProfileIconId + ".png");
  DisplaySummonerIcon();
  DisplaySummonerInformation();
};

asyncawaitの詳細については、私のブログを参照してください。

于 2016-12-17T23:08:18.750 に答える