All pastes #974137 Raw Edit

Mine

public text v1 · immutable
·published 2008-04-06 16:15 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Text.RegularExpressions;

namespace ThreadTest
{
    public class console
    {
        static void Main()
        {
            ThreadTest foo = new ThreadTest();
            try
            {
                foo.run();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("Ended");
            Console.Read();
        }
    }


    public class ThreadTest
    {
        private Queue<System.Uri> queue = new Queue<Uri>();
        public static int threadCount = 0;

        public ThreadTest()
        {
            this.queue.Enqueue(new Uri("http://www.hoffie.info"));
            this.queue.Enqueue(new Uri("http://www.dmoz.org/"));
        }

        public void run()
        {
            System.Uri u;

            while (queue.Count > 0)
            {
                u = queue.Dequeue();
                UriWorker w = new UriWorker(u, this.queue);

                ThreadPool.QueueUserWorkItem(new WaitCallback(w.process));
            }
            if (queue.Count > 0 || !Interlocked.Equals(ThreadTest.threadCount, 0))
            {
                System.Threading.Thread.Sleep(5000);
                Console.WriteLine("Sleeped for 5s");
                this.run();
            }
        }
    }

    public class UriWorker
    {
        private WebClient web = new WebClient();
        private Uri uri;
        private Queue<System.Uri> queue;
        private string reply;


        public UriWorker(Uri u, Queue<System.Uri> q)
        {
            Console.WriteLine("Constructing: " + u);
            this.uri = u;
            this.queue = q;
            Interlocked.Increment(ref ThreadTest.threadCount);
        }


        public void process(Object stateInfo)
        {
            try
            {
                this.processUri();
                this.processResponse();

                Console.WriteLine("Processed: " + this.uri);
            }
            catch (WebException e)
            {
                Console.WriteLine("WebException: " + e.Message);
            }
            finally
            {
                Interlocked.Decrement(ref ThreadTest.threadCount);
            }
        }

        private void processUri()
        {
            Console.WriteLine("Processing: " + this.uri);
            this.reply = this.web.DownloadString(this.uri);
        }

        private void processResponse()
        {
            Uri match;
            string pattern = @"href=""(http://(.*?))""";

            foreach (Match m in Regex.Matches(reply, pattern))
            {
                GroupCollection groups = m.Groups;

                try
                {
                    match = new Uri(groups[1].ToString());
                }
                catch (System.UriFormatException e)
                {
                    Console.WriteLine("UriFormatException" + e.Message);
                    continue;
                }

                this.queue.Enqueue(match);
            }
        }
    }
}