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
the problem arises in the following function when called.
And the below is the given class which calls the above usercontrol called view.
If any one can help.
Thank you,
Regards,
Syed Ahmed Hussain
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
Code:
private List<string> getData(string sensorID) { cmdText = "SELECT ID, SensorID, Temperature, Humidity, Light, SoilMoisture, ReceivingDateTime " + " FROM SensorValues SV" + " where SV.ID in (SELECT max(ID) From SensorValues WHERE SensorID LIKE '" + sensorID + "')"; sqlCon = new SqlConnection(sqlString); sqlCom = new SqlCommand(cmdText, sqlCon); try { sqlCon.Open(); rd = sqlCom.ExecuteReader(); while (rd.Read()) { values.Add(rd["SensorID"].ToString()); values.Add(rd["Temperature"].ToString()); values.Add(rd["Humidity"].ToString()); values.Add(rd["Light"].ToString()); values.Add(rd["SoilMoisture"].ToString()); values.Add(rd["ReceivingDateTime"].ToString()); } rd.Close(); } catch (Exception args) { MessageBox.Show("Error : " + args); } finally { sqlCon.Close(); } return values; }
Code:
public void setValues(string sensorID) { values = this.getData(sensorID); textBox1.Text = values[0]; textBox2.Text = values[1]; textBox3.Text = values[2]; textBox4.Text = values[3]; textBox5.Text = values[4]; textBox6.Text = values[5]; }
Code:
List<View> L = new List<View>(); View V; public static int total = 7; public static String[] SensorIDs = new String[] { "01F4", "0001", "0002", "0003", "0004", "0005", "0006" }; Terminal T = new Terminal(); public AdminUserControl1() { InitializeComponent(); //panel2.Visible = false; this.Load += new EventHandler(AdminUserControl1_Load); } void AdminUserControl1_Load(object sender, EventArgs e) { /*View usercontrol is populated in this function*/ double hMaxBy2 = (double)1024 / 2; double yMaxBy3 = (double)768/3; int x=0,y=0; for (int i = 1; i <= total; i++) { if (i == 3 || i == 5 || i == 9) { x = 0; y++; } if (i == 7) { x = 0; y = 0; } V = new View(); V.Show(); V.SetBounds((int)(x * hMaxBy2), (int)(y * yMaxBy3), (int)hMaxBy2, (int)yMaxBy3); splitContainer1.Panel2.Controls.Add(V); L.Add(V); x++; } splitContainer1.Visible = true; } private void dataFillerOpenToolStripMenuItem_Click(object sender, EventArgs e) { System.Timers.Timer Clock = new System.Timers.Timer(); Clock.Interval = 25000; Clock.Elapsed += new System.Timers.ElapsedEventHandler(Clock_Elapsed); Clock.Start(); } void Clock_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Filler startFiller = new Filler(); ThreadStarter cs = new ThreadStarter(); ThreadStarter.main(null); for (int i = 0; i < 7; i++) { L[i].setValues(SensorIDs[i]); } } } }
Thank you,
Regards,
Syed Ahmed Hussain
Comment