Tech Off Post

Single Post Permalink

View Thread: Parallel Tasks
  • User profile image
    philjay

    this is another possible way :

    public async Task LoadData()
    {
        //load allies and players simultaneously (but don't add to list yet)
        var allies = LoadAllies();
        var players = LoadPlayers();
          
        //add allies then players (maybe do some computation first?)
        this.Allies.AddRange(await allies);
        this.Players.AddRange(await players);
    }
    
    private async Task<IEnumerable<Ally> LoadAllies()
    {
        var client = new HttpClient();
        var s = await client.GetStringAsync("http://nl" + WorldName + ".tribalwars.nl/map/ally.txt");
        return from line in s.Split("\n")
               let values = line.Split(',')
               select new Ally(values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7]);
    }
    
    private async Task<IEnumerable<Player>> LoadPlayers()
    {
        var client = new HttpClient();
        var s = await client.GetStringAsync("http://nl" + WorldName + ".tribalwars.nl/map/player.txt");
        return from line in s.Split("\n")
               let values = line.Split(',')
               select new Player(values[0], values[1], values[2], values[3], values[4], values[5]);
    }