Skip to content

Commit

Permalink
Async code optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
lezzi committed Aug 18, 2016
1 parent 01b8cf1 commit c70785b
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions StatsFetcher/ProfileFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,26 @@ namespace StatsFetcher
{
public class ProfileFetcher
{
private Game game;
private HttpClient web;
private readonly Game game;
private readonly HttpClient web = new HttpClient();

public ProfileFetcher(Game game)
{
this.game = game;
this.web = new HttpClient();
}

public async Task FetchBasicProfiles()
{
var tasks = new List<Task>();

// start all requests in parallel
foreach (var p in game.Players) {
tasks.Add(FetchBasicProfile(p));
}

foreach (var task in tasks) {
await task;
}
await Task.WhenAll(game.Players.Select(FetchBasicProfile)).ConfigureAwait(false);
}

public async Task FetchFullProfiles()
{
var tasks = new List<Task>();

// start all requests in parallel
foreach (var p in game.Players) {
tasks.Add(FetchFullProfile(p));
}

foreach (var task in tasks) {
await task;
}
await Task.WhenAll(game.Players.Select(FetchFullProfile)).ConfigureAwait(false);
}

private async Task FetchBasicProfile(PlayerProfile p)
Expand Down

0 comments on commit c70785b

Please sign in to comment.