using System.Diagnostics;
using System.Threading.Tasks;
public static void Main(string[] args)
string[] urls = { "http://images.noteges.com/archivosnoteweb/873663/Oferta/0290/Imagen/t_8736632016428131012769.jpg", "http://images.noteges.com/archivosnoteweb/2821/Oferta/2624/Imagen/t_2821201721112128952.JPG", "http://images.noteges.com/archivosnoteweb/873649/Oferta/0613/Imagen/t_87364920168131050294.jpg", "http://images.noteges.com/archivosnoteweb/2821/Oferta/2145/Imagen/t_2821201582111189597.JPG", "http://images.noteges.com/archivosnoteweb/878745/Oferta/VSO1127/Imagen/t_8787452016411175032286.JPG", "http://images.noteges.com/archivosnoteweb/878745/Oferta/VSO1124/Imagen/t_878745201632485150294.JPG", "http://images.noteges.com/archivosnoteweb/2477/Oferta/2212/Imagen/t_24772017225102534191.jpg", "http://images.noteges.com/archivosnoteweb/878745/Oferta/VSO1101/Imagen/t_878745201726192644589.JPG", "http://images.noteges.com/archivosnoteweb/2821/Oferta/2551/Imagen/t_28212016101418820588.JPG", "http://images.noteges.com/archivosnoteweb/873386/Oferta/0254/Imagen/t_87338620161013173634225.jpg" };
Stopwatch sw = new Stopwatch();
total = MethodSequencial(urls);
ShowResult(sw, total, urls);
total = MethodTaskAndWait(urls);
ShowResult(sw, total, urls);
total = MethodTaskArray(urls);
ShowResult(sw, total, urls);
private static long MethodSequencial(string[] urls)
foreach (string url in urls)
total += GetTheSize(url);
public static int GetTheSize(string url)
System.Net.WebRequest req = System.Net.HttpWebRequest.Create(url);
using (System.Net.WebResponse resp = req.GetResponse())
if (!int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
catch (System.Net.WebException ex)
Console.WriteLine("Error 404. URL: {0}", url);
private static long MethodTaskAndWait(string [] urls)
foreach (string url in urls)
Task<int> taskA = Task<int>.Run(() => GetTheSize(url));
private static long MethodTaskArray(string[] urls)
Task<int>[] taskArray = new Task<int>[urls.Length];
foreach (string url in urls)
taskArray[index] = Task<int>.Factory.StartNew(() => GetTheSize(url));
for (int i = 0; i < taskArray.Length; i++)
total += taskArray[i].Result;
private static void ShowResult(Stopwatch sw, long total, string[] urls)
Console.WriteLine("Result:");
Console.WriteLine("Time elapse: {0} milliseconds = {1:N} seconds = {2:N} minutes", sw.ElapsedMilliseconds, sw.ElapsedMilliseconds / 1000.0, sw.ElapsedMilliseconds / 60000.0);
Console.WriteLine("Number of url {0}", urls.Length);
Console.WriteLine("Total Size in Bytes: {0:N} ({1:N} MB)", total, total / 1048576.0);