-
-
Save AlbertoMonteiro/2522989 to your computer and use it in GitHub Desktop.
Async CTP exemplo 1
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.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