Here is my situation. Im trying to learn how to take full advantage of multithreading but am at a rump.

I have one listbox of usernames and it checks them on a website to see if they exist or not. If they exist it puts the username into listbox2.

Now that portion works fine. But im wondering how i can use say 5 threads to go through the list and check the names quicker. My code is as follows:

//Start
        public void button5_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < 1; i++)
                {

                   // listBox1.SelectedIndex = i;
                    Thread thread1 = new Thread(new ThreadStart(this.getSite));
                    thread1.Start();
                    while (thread1.IsAlive)
                    {
                        Application.DoEvents();
                   
                    }
                    if(!(thread1.IsAlive))
                    {
                     label2.Text = "Count: " + listBox2.Items.Count;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void getSite()
        {
            for (int j = 0; j < listBox1.Items.Count; j++)
            {

                string uri = @"http://www.website.com";
                string username = listBox1.Items[j].ToString();
                string data = "accountName=" + username;
                string source = "";

                source = getInfo(data, uri);

                if (source.Contains("nswer"))
                {
                    listBox2.Invoke(new UpdateTextCallback(this.addUsername), new object[] { username });
                }

            }
        }// END GET SITE

        //Used to invoke username into listbox2 
        public void addUsername(string user)
        {
            // Add the listbox text
            listBox2.Items.Add(user);
        }
      
        //Used to invoke username into listbox2 
        public delegate void UpdateTextCallback(string user);

        public static string getInfo(string DATA, string URL)
        {
            HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(URL);
            req.Method = "post";
            req.ContentType = "application/x-www-form-urlencoded";
            req.AllowAutoRedirect = true;

            // Encode data and post it 
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(DATA);
            req.ContentLength = DATA.Length;
            Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            // Get response
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader ResponseStream = new StreamReader(resp.GetResponseStream());
            string Source = "";
            Source = ResponseStream.ReadToEnd();

            return Source;

        }

My problem is i wouldnt know how to tell it to skip the username that is being checked by another thread. Any help is greatly appreciated.

Re: Dynamic Multithreading help 80 80

Kil the whole application doevents bit, thats a sure way to tie yourself in knots.

Have a list items you've queued up to go, fire off 5, as each of the 5 finish, take the next one off the queue and start a new thread. until you have nothing left in the queue.

At the end of each threadrun, issue an event to say "A thread finished, heres the result" ..

Re: Dynamic Multithreading help 80 80

As you PMd me to say you'd solved, this, please mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.