Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Concurrent;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- namespace ConsoleApplication4
- {
- static class Program
- {
- static readonly Uri[] Uris = Enumerable.Range(0, 250).Select(_ => new Uri("http://ya.ru/")).ToArray();
- static readonly ConcurrentQueue<Uri> Queue = new ConcurrentQueue<Uri>();
- static void Main()
- {
- Exec("DoWorkThreads(1)", whenDone => DoWorkThreads(1, whenDone));
- Exec("DoWorkThreads(5)", whenDone => DoWorkThreads(1, whenDone));
- Exec("DoWorkThreads(10)", whenDone => DoWorkThreads(1, whenDone));
- Exec("DoWorkAsync", DoWorkAsync);
- }
- static void Exec(string name, Action<Action> run)
- {
- var watch = Stopwatch.StartNew();
- var completed = new ManualResetEvent(false);
- run(() => completed.Set());
- completed.WaitOne();
- watch.Stop();
- Console.WriteLine("{0} took {1} ms", name, watch.ElapsedMilliseconds);
- }
- static void DoWorkThreads(int threadsCount, Action whenDone)
- {
- foreach (var uri in Uris)
- {
- Queue.Enqueue(uri);
- }
- var threads = new Thread[threadsCount];
- for (var i = 0; i < threadsCount; i++)
- {
- threads[i] = new Thread(ProcessingThread);
- threads[i].Start();
- }
- foreach (var thread in threads)
- {
- thread.Join();
- }
- whenDone();
- }
- static void ProcessingThread()
- {
- Uri uri;
- while (Queue.TryDequeue(out uri))
- {
- LoadUri(uri, Success, Failure);
- }
- }
- static void DoWorkAsync(Action whenDone)
- {
- var syncRoot = new object();
- var counter = Uris.Length;
- var completed = new ManualResetEvent(false);
- Action handler = () =>
- {
- lock (syncRoot)
- {
- counter--;
- if (counter > 0)
- {
- return;
- }
- }
- completed.Set();
- };
- try
- {
- foreach (var uri in Uris)
- {
- LoadUriAsync(uri, Success, Failure, handler);
- }
- completed.WaitOne();
- whenDone();
- }
- catch (Exception)
- {
- whenDone();
- }
- }
- static void LoadUri(Uri uri, Action<Uri, string> success, Action<Uri> failure)
- {
- var request = CreateRequest(uri);
- try
- {
- using (var response = (HttpWebResponse)request.GetResponse())
- {
- response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
- using (var stream = response.GetResponseStream())
- using (var reader = new StreamReader(stream, Encoding.GetEncoding(1251)))
- {
- var dataContent = reader.ReadToEnd();
- var headers = response.Headers.ToString();
- success(uri, headers + dataContent);
- }
- }
- }
- catch (Exception)
- {
- failure(uri);
- }
- }
- static void LoadUriAsync(Uri uri, Action<Uri, string> success, Action<Uri> failure, Action completed)
- {
- var request = CreateRequest(uri);
- request.BeginGetResponse(asyncResult =>
- {
- try
- {
- using (var response = (HttpWebResponse)request.EndGetResponse(asyncResult))
- {
- response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
- using (var stream = response.GetResponseStream())
- using (var reader = new StreamReader(stream, Encoding.GetEncoding(1251)))
- {
- var dataContent = reader.ReadToEnd();
- var headers = response.Headers.ToString();
- success(uri, headers + dataContent);
- }
- }
- }
- catch (Exception)
- {
- failure(uri);
- }
- finally
- {
- completed();
- }
- }, null);
- }
- static HttpWebRequest CreateRequest(Uri uri)
- {
- var request = (HttpWebRequest)WebRequest.Create(uri);
- request.CookieContainer = new CookieContainer();
- request.AllowAutoRedirect = false;
- request.Timeout = 500;
- request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.19) Gecko/2010031422 Firefox/3.0.19";
- return request;
- }
- static void Success(Uri uri, string content)
- {
- }
- static void Failure(Uri uri)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement