Click here to Skip to main content
15,938,446 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am new to threading. I am trying to send HTTP Web Request using multi threading, I am not able to acheive what I need.
My requirement is to send request to thousands of same or different websites and parse the response i get it from httpwebrequest.
In the below code, i am sending 2 simulteaneous threads, I am looking for ten simultaneously threads.

namespace threading
{
    public partial class Form1 : Form
    {
        delegate string UrlFetcher(string url);

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 1;
            UrlFetcher u = new UrlFetcher(Fetch);
            UrlFetcher u = new UrlFetcher(Fetch1);
            string pageURL = "http://www.google.com";

            while (i <= 1000)
            {
                u.BeginInvoke(pageURL, new AsyncCallback(AfterFetch), "this is state");
                i++;
                u.BeginInvoke(pageURL, new AsyncCallback(AfterFetch1), "this is state");
                i++;
                Thread.Sleep(5);
            }
        }

        static string Fetch(string pageURL)
        {
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(pageURL);
            WebReq.Method = "GET";
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            string myString = _Answer.ReadToEnd();
            return myString;
        }

        void AfterFetch(IAsyncResult result)
        {
            string a;

            AsyncResult async = (AsyncResult)result;
            UrlFetcher fetcher = (UrlFetcher)async.AsyncDelegate;
            a = fetcher.EndInvoke(result).ToString();

            Regex regx = new Regex(@"<td>([A-Za-z0-9\-]+)\.(com|net)</td>", RegexOptions.IgnoreCase);
            MatchCollection mactches = regx.Matches(a);
            foreach (Match match in mactches)
            {
                string pattern = @"<(.|\n)*?>";
                string r = Regex.Replace(match.Value, pattern, string.Empty);
                textBox3.AppendText(r);
            }
        }

        static string Fetch1(string pageURL)
        {
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(pageURL);
            WebReq.Method = "GET";
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            string myString = _Answer.ReadToEnd();
            return myString;
        }

        void AfterFetch1(IAsyncResult result)
        {
            string a;

            AsyncResult async = (AsyncResult)result;
            UrlFetcher fetcher = (UrlFetcher)async.AsyncDelegate;
            a = fetcher.EndInvoke(result).ToString();

            Regex regx = new Regex(@"<td>([A-Za-z0-9\-]+)\.(com|net)</td>", RegexOptions.IgnoreCase);
            MatchCollection mactches = regx.Matches(a);
            foreach (Match match in mactches)
            {
                string pattern = @"<(.|\n)*?>";
                string r = Regex.Replace(match.Value, pattern, string.Empty);
                textBox3.AppendText(r);
            }
        }
    }
}



If anyone would correct the above code, it is really appreciated.

Thanks
Posted

1 solution

That shouldn't even compile. You're declaring the same variable twice in the button click even handler. I think you meant to declare u and u1.

Second, you're not spawning threads.

Third, 10 threads may be too much. Consider using a thread pool and do two or three at a time.

Here's an article about using thread pools:

Multithreading, Delegates, and Custom Events[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900