Multithreading errors in C #

I am trying to update a text box. I thought my thread code would fix the problem, but it is not. Can anyone help with this?

new Thread((ThreadStart)delegate { txtCapacitance.Text = Math.Round(capacitance, 3).ToString(); }).Start(); 

Gives the following error:

Cross-threading is invalid: The txtCapacitance control is accessible from a thread other than the thread that was created on.

Note that this all starts with the dataReceived function, which is called whenever USB data is received.

+6
multithreading c #
source share
5 answers

User interface objects can only be called from a user interface thread. This can be done using the Invoke control method:

 txtCapacitance.Invoke((Action)(() => { txtCapacitance.Text = Math.Round(capacitance, 3).ToString(); })); 
+6
source share

You better take a look at using the BackgroundWorker Class

Take a look

C # BackgroundWorker Tutorial .

+4
source share

Updates to any user interface element must be performed in the user interface. You can, of course, calculate your value in another thread, but you must use the .Invoke(...) method to perform the update.

+1
source share

try this one

 //delegate delegate void updateTextboxDelegate(string value); private void updateDisplay(string value) { txtCapacitance.Text = value; } //the thread string msg = string.empty; var th =new Thread(()=>{BeginInvoke(new updateTextboxDelegate(updateDisplay), msg); }); th.Start(); 

Hope this works for you.

0
source share

You need to make sure that you are updating the textBox from the same stream that you created.

To do this, create yourself as such a class helper and use these extensions instead of the usual one (here are a few extensions I made for myself, but changeText is interesting to you):

 public static class ControlExtensions { public static void changeStatus(this Control varControl, bool varState) { if (varControl.InvokeRequired) { varControl.BeginInvoke(new MethodInvoker(() => changeStatus(varControl, varState))); } else { varControl.Enabled = varState; } } public static void changeText(this Control varControl, string varText) { if (varControl.InvokeRequired) { varControl.BeginInvoke(new MethodInvoker(() => changeText(varControl, varText))); } else { varControl.Text = varText; } } public static DateTime readDateValue(this DateTimePicker varControl) { if (varControl.InvokeRequired) { return (DateTime) varControl.Invoke(new Func<DateTime>(() => readDateValue(varControl))); } else { return varControl.Value; } } public static bool ReadStatus(this CheckBox varControl) { if (varControl.InvokeRequired) { return (bool) varControl.Invoke(new Func<bool>(() => ReadStatus(varControl))); } return varControl.Checked; } public static bool ReadStatus(this RadioButton varControl) { if (varControl.InvokeRequired) { return (bool) varControl.Invoke(new Func<bool>(() => ReadStatus(varControl))); } return varControl.Checked; } public static string readText(this Control varControl) { if (varControl.InvokeRequired) { return (string) varControl.Invoke(new Func<string>(() => readText(varControl))); } else { return varControl.Text; } } public static bool readEnabled(this Control varControl) { if (varControl.InvokeRequired) { return (bool) varControl.Invoke(new Func<bool>(() => readEnabled(varControl))); } else { return varControl.Enabled; } } } 

Then you use it as follows: txtCapacitance.changeText("new text");

0
source share

All Articles