using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleParallel
{
class Program
{
static void Main(string[] args)
{
Process().GetAwaiter().GetResult();
Console.ReadKey();
}
static async Task Process()
{
var urls = new[]
{
"https://github.com/naudio/NAudio",
"https://github.com/",
"https://pluralsight.com/",
"https://github.com/",
"http://stackoverflow.com/users/7532/mark-heath",
"https://mvp.microsoft.com/en-us/",
"http://www.sergey.co.uk/",
"http://www.sergey.co.uk/links",
};
var client = new HttpClient();
var maxThreads = 4;
var q = new ConcurrentQueue<string>(urls);
var tasks = new List<Task>();
for (int n = 0; n < maxThreads; n++)
{
tasks.Add(Task.Run(async () => {
while (q.TryDequeue(out string url))
{
var html = await client.GetStringAsync(url);
Console.WriteLine($"retrieved {html.Length} characters from {url}");
}
}));
}
await Task.WhenAll(tasks);
}
}
}