Skip to content

Instantly share code, notes, and snippets.

@rahulsahay19
Created November 6, 2023 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsahay19/b87768715b93023d87710f9e298b3b68 to your computer and use it in GitHub Desktop.
Save rahulsahay19/b87768715b93023d87710f9e298b3b68 to your computer and use it in GitHub Desktop.
WhenAllExample
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Generic;
public class WhenAllExample
{
public static async Task Main(string[] args)
{
List<Task<string>> downloadTasks = new List<Task<string>>
{
DownloadAsync("https://example.com/page1"),
DownloadAsync("https://example.com/page2"),
DownloadAsync("https://example.com/page3")
};
// Wait for all downloads to complete
string[] results = await Task.WhenAll(downloadTasks);
Console.WriteLine("Downloads completed:");
foreach (string result in results)
{
Console.WriteLine(result);
}
}
public static async Task<string> DownloadAsync(string url)
{
using (HttpClient client = new HttpClient())
{
return await client.GetStringAsync(url);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment