Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Forked from alexsandro-xpt/gist:2522945
Created April 29, 2012 00:55
Show Gist options
  • Save AlbertoMonteiro/2522989 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/2522989 to your computer and use it in GitHub Desktop.
Async CTP exemplo 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
private static int total;
static void Main(string[] args)
{
IList<Uri> uris = new List<Uri>
{
new Uri("http://www.google.com"),
new Uri("http://www.microsoft.com"),
new Uri("http://www.devbrasil.net"),
new Uri("http://www.yahoo.com"),
new Uri("http://www.gmail.com")
};
var task = SumPageSizesAsync(uris);
Console.WriteLine(task);
Console.ReadLine();
}
public static int SumPageSizesAsync(IList<Uri> uris)
{
var tasks = new List<Task>();
foreach (var uri in uris)
{
Console.WriteLine("Verificando: {0}", uri);
tasks.Add(Total(uri));
}
while (!tasks.All(x => x.IsCompleted)) {}
return total;
}
private static async Task Total(Uri uri)
{
var data = await new WebClient().DownloadDataTaskAsync(uri);
total += data.Length;
Console.WriteLine("\tVerificado: {0}\n", uri);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment