By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

Error: "Cross-thread operation not valid" Problem here

P: 79
hey everyone...
I have a code which gives me this error.

Error : Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.

I am actually populating a user control dynamically. And the problem arises when i try to populate it with values. and the code runs again after 30 or more seconds to get updated values and so on..

The code is given below
This is actually the user control which should be pupulated dynamically and provide values according to the query...This user control is known as view
Expand|Select|Wrap|Line Numbers
  1.  private List<string> getData(string sensorID)
  2.         {
  3.             cmdText = "SELECT ID, SensorID, Temperature, Humidity, Light, SoilMoisture, ReceivingDateTime " +
  4.                       " FROM SensorValues SV" + " where SV.ID in (SELECT max(ID) From SensorValues WHERE SensorID LIKE '" + sensorID + "')";
  5.             sqlCon = new SqlConnection(sqlString);
  6.             sqlCom = new SqlCommand(cmdText, sqlCon);
  7.  
  8.             try
  9.             {
  10.                 sqlCon.Open();
  11.                 rd = sqlCom.ExecuteReader();
  12.                 while (rd.Read())
  13.                 {
  14.                     values.Add(rd["SensorID"].ToString());
  15.                     values.Add(rd["Temperature"].ToString());
  16.                     values.Add(rd["Humidity"].ToString());
  17.                     values.Add(rd["Light"].ToString());
  18.                     values.Add(rd["SoilMoisture"].ToString());
  19.                     values.Add(rd["ReceivingDateTime"].ToString());
  20.                 }
  21.                 rd.Close();
  22.  
  23.             }
  24.             catch (Exception args)
  25.             {
  26.                 MessageBox.Show("Error : " + args);
  27.             }
  28.             finally
  29.             {
  30.                 sqlCon.Close();
  31.             }
  32.             return values;
  33.  
  34.         }
  35.  
the problem arises in the following function when called.
Expand|Select|Wrap|Line Numbers
  1.         public void setValues(string sensorID)
  2.         {
  3.             values = this.getData(sensorID);
  4.             textBox1.Text = values[0];
  5.             textBox2.Text = values[1];
  6.             textBox3.Text = values[2];
  7.             textBox4.Text = values[3];
  8.             textBox5.Text = values[4];
  9.             textBox6.Text = values[5];
  10.         }
  11.  
  12.  
  13.  
And the below is the given class which calls the above usercontrol called view.
Expand|Select|Wrap|Line Numbers
  1. List<View> L = new List<View>();
  2.  
  3.         View V;
  4.         public static int total = 7;
  5.         public static String[] SensorIDs = new String[] { "01F4", "0001", "0002", "0003", "0004", "0005", "0006" };
  6.         Terminal T = new Terminal();
  7.  
  8.         public AdminUserControl1()
  9.         {
  10.             InitializeComponent();
  11.             //panel2.Visible = false;
  12.             this.Load += new EventHandler(AdminUserControl1_Load);
  13.         }
  14.  
  15.         void AdminUserControl1_Load(object sender, EventArgs e)
  16.         {
  17. /*View usercontrol is populated in this function*/
  18.  
  19.             double hMaxBy2 = (double)1024 / 2;
  20.             double yMaxBy3 = (double)768/3;
  21.  
  22.             int x=0,y=0;
  23.  
  24.             for (int i = 1; i <= total; i++)
  25.             {
  26.  
  27.  
  28.                 if (i == 3 || i == 5 || i == 9)
  29.                 {
  30.                     x = 0;
  31.                     y++;
  32.                 }
  33.                 if (i == 7)
  34.                 {
  35.                     x = 0;
  36.                     y = 0;
  37.                 }
  38.  
  39.                 V = new View();
  40.  
  41.                 V.Show();
  42.                 V.SetBounds((int)(x * hMaxBy2), (int)(y * yMaxBy3), (int)hMaxBy2, (int)yMaxBy3);
  43.                 splitContainer1.Panel2.Controls.Add(V);
  44.                 L.Add(V);
  45.                 x++;
  46.             }
  47.  
  48.             splitContainer1.Visible = true;
  49.         }
  50.  
  51.  
  52.         private void dataFillerOpenToolStripMenuItem_Click(object sender, EventArgs e)
  53.         {
  54.             System.Timers.Timer Clock = new System.Timers.Timer();
  55.             Clock.Interval = 25000;
  56.  
  57.             Clock.Elapsed += new System.Timers.ElapsedEventHandler(Clock_Elapsed);
  58.  
  59.             Clock.Start();
  60.  
  61.         }
  62.  
  63.         void Clock_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  64.         {
  65.             Filler startFiller = new Filler();
  66.             ThreadStarter cs = new ThreadStarter();
  67.             ThreadStarter.main(null);
  68.  
  69.             for (int i = 0; i < 7; i++)
  70.             {
  71.  
  72.                 L[i].setValues(SensorIDs[i]);
  73.             }
  74.  
  75.  
  76.         }
  77.  
  78.     }    
  79. }
  80.  
  81.  
If any one can help.

Thank you,

Regards,
Syed Ahmed Hussain
Aug 3 '09 #1
Share this Question
Share on Google+
11 Replies


MrMancunian
Expert 100+
P: 569
Did you set breakpoints to see at what point you get the error message? If not, please do so and tell us which line gives you the error.

Steven
Aug 3 '09 #2

P: 79
Yes I did set up the break points .... It gave me an error on the following line. :

Expand|Select|Wrap|Line Numbers
  1. textBox1.Text = values[0];
  2.  
Aug 3 '09 #3

P: 79
I dont know how but when I called the function it got 12 values in it as well..

regards,
Syed Ahmed Hussain
Aug 3 '09 #4

tlhintoq
Expert 2.5K+
P: 3,525
Error : Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
You created the textbox on one thread... and are now trying to change its values on another thread. You can't do that.

Take a look at Method Invoker. You can create an invoker (which works across thread barriers) which will invoke your control's add method to add your values.
Aug 3 '09 #5

P: 79
You can create an invoker (which works across thread barriers) which will invoke your control's add method to add your values.
Please provide me an example

Thank you,

Regards,
Syed Ahmed Hussain
Aug 3 '09 #6

tlhintoq
Expert 2.5K+
P: 3,525
Please provide me an example
Take a look at the MSDN for method invoker.
If after giving it a try on your own it still doesn't work, post the code you have created/tried and we'll see if we can't find where you went wrong.
Aug 3 '09 #7

P: 79
Error: " Exception has been thrown by the target of an invocation." at program.cs
on line :
Expand|Select|Wrap|Line Numbers
  1. Application.Run(new Startup());
Aug 4 '09 #8

P: 79
I have done this..
Expand|Select|Wrap|Line Numbers
  1.   public delegate void MethodInvoker();
  2.         public void setValues(string sensorID)
  3.         {
  4.             MethodInvoker invoker1 = new MethodInvoker(delegate()
  5.             {
  6.                 values = this.getData(sensorID);
  7.                 textBox1.Text = values[0];
  8.                 textBox2.Text = values[1];
  9.                 textBox3.Text = values[2];
  10.                 textBox4.Text = values[3];
  11.                 textBox5.Text = values[4];
  12.                 textBox6.Text = values[5];
  13.             }
  14.             );
  15.             this.BeginInvoke(invoker1);
  16.         }
and I am getting the error : "{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}"
Aug 4 '09 #9

MrMancunian
Expert 100+
P: 569
Sounds like you forgot to subtract 1 from a collection...Collections start at 0.

Steven
Aug 4 '09 #10

P: 79
@MrMancunian :

Thanks buddy. But I found the problem. The problem lies in the database as well. Their wasnt any value with the name of the parameter that was given to it.
Then I realize that there should be a condition of count to work with. If there is a count then it will simply map the values, else it wont.

Thank you every one.

:)

Regards,

Syed Ahmed Hussain
Aug 4 '09 #11

MrMancunian
Expert 100+
P: 569
@Ahmedhussain
//Offtopic: Woohoo! I'm someone's buddy! :-D

//Ontopic: Good job :-)
Aug 4 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.