vb.net - Cross-thread operation not valid -


i doing following try out threadings 'cross-threading' error in line txtbox.text = datagridview2.rows.item(iloop).cells(3).value, can point out what's wrong, thanks:

dim lm new thread(addressof load_movie)         dim lt new thread(addressof load_timings)         lm.start()         lt.start()  private sub load_movie()          iloop integer = 0 datagridview1.rows.count - 2             dim cstring string = "txt_movie_0" & iloop.tostring             each cctrl control in panel1.controls                 if typeof cctrl textbox                     dim txtbox textbox                     txtbox = cctrl                     if txtbox.name = cstring                         txtbox.text = datagridview1.rows.item(iloop).cells(1).value                     end if                 end if             next         next     end sub      private sub load_timings()         iloop integer = 0 datagridview2.rows.count - 2             each cctrl control in panel2.controls                 if typeof cctrl textbox                     dim txtbox textbox                     txtbox = cctrl                     if (txtbox.name.substring(9, 6)) = (datagridview2.rows.item(iloop).cells(0).value.substring(0, 6))                         txtbox.text = datagridview2.rows.item(iloop).cells(3).value 'this part says "cross-thread operation not valid: control 'txt_time_00_000' accessed thread other thread created on."                     end if                 end if             next         next      end sub 

@jaredpar have basic idea code won't compile (unless i'm missing something). vb9 or less need declare actual delegate , invoke that:

    ''//the delegate needed vb 9 or less version     private delegate sub updatetextboxdelegate(byval txtbox textbox, byval value string)      if typeof cctrl textbox         dim txtbox textbox         txtbox = cctrl         ''//perform validation logic here         if (txtbox.name.substring(9, 6)) = (datagridview2.rows.item(iloop).cells(0).value.tostring().substring(0, 6))             ''//call update method our textbox , value             updatetextbox(txtbox, datagridview2.rows.item(iloop).cells(3).value.tostring())         end if     end if      private sub updatetextbox(byval txtbox textbox, byval value string)         ''//basically ask textbox if need invoke         if txtbox.invokerequired             ''//for vb 9 or less need delegate             txtbox.invoke(new updatetextboxdelegate(addressof updatetextbox), txtbox, value)         else             txtbox.text = value         end if     end sub 

for vb 10 can use anonymous subs can rid of delegate:

    if typeof cctrl textbox         dim txtbox textbox         txtbox = cctrl         ''//perform validation logic here         if (txtbox.name.substring(9, 6)) = (datagridview2.rows.item(iloop).cells(0).value.tostring().substring(0, 6))             ''//call update method our textbox , value             updatetextbox(txtbox, datagridview2.rows.item(iloop).cells(3).value.tostring())         end if     end if  private sub updatetextbox(byval txtbox textbox, byval value string)     if txtbox.invokerequired         ''//for vb 10 can use anonymous sub         txtbox.invoke(sub() updatetextbox(txtbox, value))     else         txtbox.text = value     end if end sub 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -