3 Replies - 5820 Views - Last Post: 12 September 2011 - 03:16 AM Rate Topic: -----

#1 kytro360  Icon User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Cross Thread Operation Not Valid

Posted 11 September 2011 - 05:28 PM

Im trying to run this piece of code in threads but I keep getting this error: Cross Thread Operation Not Valid

This is the code I am working with:

                        for (int o = 0; o < numericUpDown3.Value; o++)
                        {


                            for (int i = 0; i < numericUpDown2.Value; i++)
                            {
                                listBox4.SetSelected(i, true);
                                listBox4.SelectedItem.ToString();
                                listBox5.SetSelected(i, true);
                                listBox5.SelectedItem.ToString();


                            Thread t = new Thread(createaccount);
                            t.Start();
                        }



Is This A Good Question/Topic? 0
  • +

Replies To: Cross Thread Operation Not Valid

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,659
  • Joined: 02-June 10

Re: Cross Thread Operation Not Valid

Posted 12 September 2011 - 02:59 AM

Your listboxes were created on a different thread than this code is executing on.
Was This Post Helpful? 0
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Cross Thread Operation Not Valid

Posted 12 September 2011 - 03:02 AM

You must be updating UI controls from your createaccount() method, or from a another method that createaccount() calls. You cannot update UI controls from any thread other than the thread that the controls were created on.

If you need to update controls from that other thread, you need to use Invoke() to marshall the update back onto the UI thread.

For example, if I wanted to update a textbox called myTextBox, and I knew that I was on a thread other than the UI thread, I could do this:

this.myTextBox.Invoke(new MethodInvoker(() => this.myTextBox.Text = "Update!"));

This post has been edited by CodingSup3rnatur@l-360: 12 September 2011 - 03:10 AM
Reason for edit:: Got Action and MethodInvoker mixed up

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,659
  • Joined: 02-June 10

Re: Cross Thread Operation Not Valid

Posted 12 September 2011 - 03:16 AM

Completely right.

But you should also look at the logic. Don't tightly bind a process on one thread to GUI controls from a different thread.

The CreateAccounts thread (and form?) shouldn't know a thing about your first form. It should just raise an event that says "NewUser(userObject)" and let your first form hear that and react the way it is programmed to. It is not the job of your CreateAccounts thread to manage some other thread's GUI. It should do just its job and leave the other threads/forms to do their jobs.

Any time I have to consider Invoke, my first step is to see what I fraked up in the logic and the separation of forms/threads. Sure there is a place for it, but most times you should just be changing property values and GUI. Let the property change the GUI if that's its job.

Also... what the heck is this?
listBox4.SelectedItem.ToString();


.SelectedItem returns an item. You're taking that and getting a string. But you aren't doing anything with it or assigning it to anything. This is just wasted CPU cycles. It's like half of a sentence. If the selected item in the listbox were "Fluffy the dog" then this line of code litterly is just...

08 "Fluffy the dog"

With no purpose or function in life.

This loop is going to just ZOOM. You are going to go through those 1,000 items in your list box in a heartbeat. But you aren't sending any data to your createaccount method. If you are relying on createaccound to look back at the selected item of the listbox you can forget about it. By the time that method is running on its own thread, you have created 50 more threads and moved the selected item of the listboxes 50 rows down. If your createaccount needs information from the listboxes then you need to send it when you create the thread. It needs to be a ParameterizedThreadStart so you can send parameters to be used when it starts.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1