c# - Cross Threading not allow -


i keep getting error while debugging code. new programming , have tried solutions have stated similar problems , still can not solve problem.

the exception being thrown is:

cross-thread operation not valid: control 'textbox2' accessed thread other thread created on.

private void send_data(int i)     {          textbox2.text = "";          int cnt = 0;         int old_cnt;         if (i == 1)         {              textbox2.text += textbox1.lines[cnt];               regex gcode = new regex("[ngxyzf][+-]?[0-9]*\\.?[0-9]*", regexoptions.ignorecase);             matchcollection m = gcode.matches(this.textbox2.text);               double x, y, z, f;               int g_code = 0;             int x_code = 0, y_code = 0, z_code = 0, x_int1 = 0, x_int2 = 0, y_int1 = 0, y_int2 = 0, z_int1 = 0, z_int2 = 0;             float x = 0, y = 0, z = 0, x_float = 0, y_float = 0, z_float = 0;               foreach (match n in m)             {                  if (n.value.startswith("g"))                 {                     g_code = convert.toint32(extractnumbers(n.value));                 }                  if (n.value.startswith("x"))                 {                     x = float.parse(extractnumbers(n.value));                     x_int1 = (int)x;                     x_int2 = x_int1;                     x_float = x - (float)x_int1;                     x_float = x_float * 1000;                     x_code = (int)x_float;                  }                  if (n.value.startswith("y"))                 {                     y = float.parse(extractnumbers(n.value));                     y_int1 = (int)y;                     y_int2 = y_int1;                     y_float = y - (float)y_int1;                     y_float = y_float * 1000;                     y_code = (int)y;                  }                  if (n.value.startswith("z"))                 {                     z = float.parse(extractnumbers(n.value));                     z_int1 = (int)z;                     z_int2 = z_int1;                     z_float = z - (float)z_int1;                     z_float = z_float * 1000;                     z_code = (int)z;                   }               }              = 0;             //write_data = 0;             exchangeinputandoutputreports(g_code, x_code, y_code, z_code, x_int2, y_int2, z_int2);             //textbox2.text = "";             cnt++;          }         //textbox2.text = "";         //cnt++;     } 

i calling function following function

public void getinputreportdata( iasyncresult ar )      {                      string bytevalue = null;          int32 count = 0;          byte[] inputreportbuffer = null;          boolean success = false;         //int test = 0;         int32 write_data = 0;          try          {               inputreportbuffer = (byte[])ar.asyncstate;               filestreamdevicedata.endread(ar);              tmrreadtimeout.stop();              if ( ( ar.iscompleted ) )              {                 mymarshaltoform("additemtolistbox", "an input report has been read.");                                      mymarshaltoform( "additemtolistbox", " input report id: " + string.format( "{0:x2} ", inputreportbuffer[ 0 ] ) );                                      mymarshaltoform( "additemtolistbox", " input report data:" );                   ( count=0; count <= inputreportbuffer.length -1 ; count++ )                  {                                              //  display bytes 2-character hex strings.                      bytevalue = string.format( "{0:x2} ", inputreportbuffer[ count ] );                       mymarshaltoform( "additemtolistbox", " " + bytevalue );                      mymarshaltoform( "additemtotextbox", bytevalue );                                          }                  //------------------------------------------------------------                  write_data = convert.toint32(inputreportbuffer[1]);                 if (write_data == 1)                 {                      //lbltest.text = convert.tostring(write_data);                     send_data(1);                 }                 //test = 1;                                   }              else              {                  mymarshaltoform( "additemtolistbox", "the attempt read input report has failed." );                  debug.write( "the attempt read input report has failed" );                                 }               mymarshaltoform( "scrolltobottomoflistbox", "" );               //  enable requesting transfer.              mymarshaltoform( "enablecmdonce", "" );             transferinprogress = false;           }          catch ( exception ex )          {              displayexception( this.name, ex );              throw ;          }                  }  

when calling first function getting cross threading not allowed error , have tried invoke method well. can not properly. can tell me solution problem

please note mentions @joe

try this:

        action action = () => textbox2.text = "";         textbox2.invoke(action); 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -