User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 456,472 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,778 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 346 | Replies: 2 | Solved
Reply
Join Date: Oct 2007
Posts: 7
Reputation: blindkilla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
blindkilla blindkilla is offline Offline
Newbie Poster

Dynamic Multithreading help

  #1  
Sep 29th, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2008
Posts: 872
Reputation: LizR will become famous soon enough LizR will become famous soon enough 
Rep Power: 3
Solved Threads: 79
LizR LizR is offline Offline
Practically a Posting Shark

Re: Dynamic Multithreading help

  #2  
Sep 29th, 2008
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" ..
Reply With Quote  
Join Date: Aug 2008
Posts: 872
Reputation: LizR will become famous soon enough LizR will become famous soon enough 
Rep Power: 3
Solved Threads: 79
LizR LizR is offline Offline
Practically a Posting Shark

Re: Dynamic Multithreading help

  #3  
Sep 30th, 2008
As you PMd me to say you'd solved, this, please mark this thread as solved.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 2:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC