static void Main(string[] args) { string[] urls = { "http://www.microsoft.com", "http://www.contoso.com", "http://www.msn.com", "http://msdn.microsoft.com", "http://mspress.microsoft.com" }; AutoResetEvent[] waithandles = new AutoResetEvent[urls.Length]; // Record the time before we start DateTime beginTime = DateTime.Now; // Retrieve each page for (int i = 0; i < urls.Length; i++) { waithandles[i] = new AutoResetEvent(false); PageSize ps = new PageSize(urls[i], waithandles[i], new ResultDelegate(ResultCallback)); Thread t = new Thread(new ThreadStart(ps.GetPageSize)); t.Name = "Thread" + (i + 1).ToString(); t.Start(); //ThreadPool.QueueUserWorkItem(GetPage, ti); } //Wait all the events WaitHandle.WaitAll(waithandles); // Display the elapsed time Console.WriteLine("Time elapsed: " + (DateTime.Now - beginTime).ToString()); Console.WriteLine("Press a key to continue"); Console.ReadKey(); }
static void ResultCallback(PageSize ps) { Console.WriteLine("{0}: {1}", ps.url, ps.bytes.ToString()); }