I am trying to update a text box. I thought my threading code would fix the problem, but it does not. Can anyone help with this?
new Thread((ThreadStart)delegate { txtCapacitance.Text = Math.Round(capacitance, 3).ToString(); }).Start();
Gives the following error:
Cross-thread operation not valid: Control 'txtCapacitance' accessed from a thread other than the thread it was created on.
Note that all this is being started by a dataReceived function which is called whenever USB data is received.
UI objects can only be called from the UI thread. This can be accomplished via the Control's Invoke
method:
txtCapacitance.Invoke((Action)(() => { txtCapacitance.Text = Math.Round(capacitance, 3).ToString(); }));
You should rather have a look at using BackgroundWorker Class
Have a look at
C# BackgroundWorker Tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With