Created
November 6, 2023 18:38
-
-
Save rahulsahay19/b87768715b93023d87710f9e298b3b68 to your computer and use it in GitHub Desktop.
WhenAllExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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