c# - Cross Thread after 20 seconds running -


i receive data serial port , show received data in format arrive in multiline text box. serial monitor "txtoutput". after have validated data complete messages , filtered out few characters (in separate class), need show data additional text boxes (textbox7, 2 , 6). reason not able understand, after 10 20 seconds of debugging, messages coming in continuously (200-300 ms), cross thread error, "cross-thread operation not valid: control accessed thread other thread created on".

this code:

serial port reading

 private void serialport1_datareceived(object sender, system.io.ports.serialdatareceivedeventargs e)         {             try             {                 settext(serialport1.readexisting());             }             catch (exception ex)             {                 settext(ex.tostring());             }         } 

delegate settext

delegate void settextcallback(string text); 

receive serial port data , elaborate

  private void settext(string text)         {             if (this.txtoutput.invokerequired)             {              settextcallback d = new settextcallback(settext);              this.begininvoke(d, new object[] { text });             }             else             {                txtoutput.appendtext(text);             }                string a="", b="", c="";                     string invia = text.tostring();                     stripper strp = new stripper();                     strp.distri(invia, out a, out b, out c);                     textbox7.text = a; // current                     textbox2.text = b; //temperature                     textbox6.text = c; //rpm                }            } 

this 1 of first app. need change avoid cross thread error , mistake made?

you've got code changes ui elements outside part protected invokerequired. text boxes need protected too.

try this:

private void settext(string text) {     if (this.txtoutput.invokerequired)     {         settextcallback d = new settextcallback(settext);         this.begininvoke(d, new object[] { text });     }     else     {         txtoutput.appendtext(text);         string a="", b="", c="";         string invia = text.tostring();         stripper strp = new stripper();         strp.distri(invia, out a, out b, out c);         textbox7.text = a; // current         textbox2.text = b; //temperature         textbox6.text = c; //rpm     } }            

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -