· 9 years ago · Oct 21, 2016, 11:16 AM
1 List<string> addresses = new List<string>()
2 {
3 "google.ru",
4 "yandex.ru",
5 "rambler.ru",
6 "bing.com",
7 "ridus.ru",
8 "habrahabr.ru",
9 "geektimes.ru",
10 "mail.ru",
11 "2ch.hk",
12 "iichan.hk",
13 "dobrochan.ru",
14 "youtube.com",
15 "postnauka.ru",
16 "nplus1.ru",
17 "elementy.ru",
18 "membrana.ru",
19 "antropogenez.ru",
20 "histrf.ru",
21 "iddqd.ru"
22 };
23
24 //ТеÑÑ‚ Пинга
25 //Ping pinger = new Ping();
26 //var tasks = addresses.Select(async address =>
27 //{
28 // bool success = false;
29 // PingReply reply = null;
30 // try
31 // {
32 // // Пока одну задачу не завершит, оÑтальные выдаёт Ñ Ð¾ÑˆÐ¸Ð±ÐºÐ¾Ð¹ "задача уже иÑполнÑетÑÑ"
33 // //reply = await pinger.SendPingAsync(address);
34 // reply = await new Ping().SendPingAsync(address); // а так пингует вÑÑ‘
35 // success = true;
36 // }
37 // catch (Exception e)
38 // {
39 // success = false;
40 // }
41 // Console.WriteLine(string.Concat(address, " ", (success) ? reply.Status.ToString() : "fail"));
42 //}).ToArray();
43 //Task.WhenAll(tasks).Wait();
44
45 //ТеÑÑ‚ HttpClient
46 addresses = addresses.Select(x => "http://" + x).ToList();
47 HttpClient httpClient = new HttpClient();
48 var tasks = addresses.Select(async address =>
49 {
50 bool success = false;
51 HttpResponseMessage response = null;
52 try
53 {
54 response = await httpClient.GetAsync(address); // Оба варианта работают одинаково
55 //response = await new HttpClient().GetAsync(address); //
56 success = true;
57 }
58 catch (Exception e)
59 {
60 success = false;
61 }
62 Console.WriteLine(string.Concat(address, " ", (success) ? response.StatusCode.ToString() : "fail"));
63 }).ToArray();
64 Task.WhenAll(tasks).Wait();
65
66
67 Console.WriteLine("задачи завершены");
68 Console.ReadKey();