var queue = new Queue<Task>();
while (true)
{
if (queue.Count >= 50)
{
var task = queue.Dequeue();
task.Wait();
task.Dispose();
}
var request = (HttpWebRequest) WebRequest.Create(url);
var watch = Stopwatch.StartNew();
queue.Enqueue(Task.Factory
.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null)
.ContinueWith(task =>
{
using (var response = (HttpWebResponse) task.Result)
{
Console.WriteLine("{0}\t{1}\t{2}",
response.StatusCode, response.ContentType, watch.Elapsed);
}
}));
}
Task Parallel Library (TPL)
No comments:
Post a Comment