Hello, I am trying to multithread a listBox which has URL's in it.

Basically what I am trying to do is to make it so each thread I make, will visit one of the URL's in the listBox and do a GET request.

I was wondering how do I make it so each thread I make, visit a different site in the list, not the same site?

For example, what happens now is, if I pick 3 threads, those 3 threads will visit the same sites 3 times.

How can I do it so if my listBox has 3 sites in the list, those 3 threads will visit a different site in the listBox so it'll finish faster, and not the same sites?

Heres the code I am working with right now. Help is appreciated:

This is the multithreading code (the code that creates the threads):

            CheckForIllegalCrossThreadCalls = false;
            var threads = new Thread[(int)numericUpDown1.Value]; 
            for (int p = 0; p < (int)numericUpDown1.Value; p++)
            {
                threads[p] = (new Thread(() => selectall()));
                threads[p].Start();
            }

And here is the method I make each thread go to:

public void selectall()
        {
            for (int i = 0; i< listBox1.Items.Count;i++)
            {
                listBox1.SetSelected(i, true);
                CheckForIllegalCrossThreadCalls = false;
                string url = listBox1.SelectedItem.ToString();
                HttpInterface http = new HttpInterface();
                http.get(url);
                MessageBox.Show(url);
                listBox1.ClearSelected();
            }

        }

Bump............

You cannot access control which were creates on one (usually on UI) thread, from another thread. You will need to Invoke control.
For more info read here: http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

FOr example to clear selection do:

listBox1..Invoke(() => listBox1.ClearSelection());

Will that solve the issue?

I am aware Im not invoking but I can worry about that later.

Be a part of the DaniWeb community

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