Tobey60
Tobey60

Reputation: 147

How can I run many asynchronous methods all together?

I want to run my asynchronous methods GetPlayerCountryData() and GetPlayerTagsData() all together to save time instead of starting the next method only after the previous has completed. But I don't know how to do that.

https://jeremylindsayni.wordpress.com/2019/03/11/using-async-await-and-task-whenall-to-improve-the-overall-speed-of-your-c-code/

I have read this tutorial but I don't know how to use await Task.WhenAll() in my code.

In addition, I want to execute the line AllMethodsCompleted = true; after all my asynchronous methods have been completed successfully. Should I use await Task.WhenAll() in this situation?

How can I only set AllMethodsCompleted = true if all my asynchronous methods completed successfully? Is it possible to find out if (result.Error != null) or an exception occurred in one of the asynchronous methods before setting AllMethodsCompleted = true ?

string PlayerDeviceId = "";

private void RegisterGuestPlayFabAccount()
{
    PlayerDeviceId = ReturnMobileID();
    var requestIOS = new LoginWithIOSDeviceIDRequest { DeviceId = PlayerDeviceId, CreateAccount = true };
    var loginTask = PlayFabClientAPI.LoginWithIOSDeviceIDAsync(requestIOS);
    loginTask.ContinueWith(OnPlayFabRegisterGuestAccountComplete);
}

private void OnPlayFabRegisterGuestAccountComplete(Task<PlayFabResult<LoginResult>> task)
{
    if (task.Result.Result != null)
    {
        PlayerAccountDetails();
    }

    if (task.Result.Error != null)
    {
        OnPlayFabError(task.Result.Error);
    }
}

 bool AllMethodsCompleted = false;

 public async void PlayerAccountDetails()
 {
     await GetPlayerCountryData();
     await GetPlayerTagsData();
     AllMethodsCompleted = true;
 }

 private async Task GetPlayerTagsData()
 {
     var resultprofile = await PlayFabServerAPI.GetPlayerTagsAsync(new PlayFab.ServerModels.GetPlayerTagsRequest()
     {
         PlayFabId = PlayerPlayFabID
     });

     if (resultprofile.Error != null)
         OnPlayFabError(result.Error);
     else
     {
         if ((resultprofile.Result != null) && (resultprofile.Result.Tags.Count() > 0))
             CurrentPlayerTag = resultprofile.Result.Tags[0].ToString();
     }
 }

 private async Task GetPlayerCountryData()
 {
     var resultprofile = await PlayFabClientAPI.GetUserDataAsync(new PlayFab.ClientModels.GetUserDataRequest()
     {
         PlayFabId = PlayerPlayFabID,
         Keys = null
     });

     if (resultprofile.Error != null)
         OnPlayFabError(result.Error);
     else
     {
         if (resultprofile.Result.Data == null || !resultprofile.Result.Data.ContainsKey("Country") || !resultprofile.Result.Data.ContainsKey("City"))
             Console.WriteLine("No Country/City");
         else
         {
             PlayerCountry = resultprofile.Result.Data["Country"].Value);
             PlayerCity = resultprofile.Result.Data["City"].Value);
         }
     }
 }

Upvotes: 0

Views: 106

Answers (1)

user10127407
user10127407

Reputation: 108

public async Task PlayerAccountDetails()
{ 
    var playerCountryData = GetPlayerCountryData());
    var playerTagsData = GetPlayerTagsData());        

    await Task.WhenAll(playerCountryData, playerTagsData);
    AllMethodsCompleted = true;
}

Here is the method you are in query about and running the 2 methods in async waiting for each to finish then moving on. They just needed to be assigned to a task variable.

Upvotes: 0

Related Questions