Vb.net multithreading not working...? -


i'm trying create multithreaded program poll machines data can't seem working correctly. code below working , creating 4 threads should flow of code seems happen in series , on main ui thread.

what i'm trying achieve each row of datagrid update simultaneously without locking ui.

below dumbed down version of have serves demonstrate problem. info 'testclass' class used instance of machine each class element representing property of machine.

hope i've given enough info explain problem. in advance.

ps shouldn't need refresh form should i?

imports system.threading  public class testform  public threadcount integer public delegate sub testclassdelegate(test object)  private class testclass     public index integer     public testval1 integer = 100     public testval2 integer = 200     public testval3 integer = 300     public testval4 integer = 400     public testval5 integer = 500     public testval6 integer = 600     public testdel testclassdelegate end class  private sub testform_load(sender object, e eventargs) handles mybase.load     integer = 0 3         datagridview1.rows.add()         datagridview1.rows(i).cells(0).value = + 1     next end sub  private sub button1_click(sender object, e eventargs) handles button1.click     integer = 0 3         datagridview1.rows(i).cells(1).value = ""         datagridview1.rows(i).cells(2).value = ""         datagridview1.rows(i).cells(3).value = ""         datagridview1.rows(i).cells(4).value = ""         datagridview1.rows(i).cells(5).value = ""         datagridview1.rows(i).cells(6).value = ""     next     poll_freethread() end sub  private sub poll_freethread()     integer = 0 datagridview1.rows.count - 1         dim test new testclass         test.index =         test.testdel = addressof uiupdate          interlocked.increment(threadcount)         me.label2.text = threadcount         try             dim thpoll new thread(sub() invokeuiupdate(test))             thpoll.isbackground = true             thpoll.priority = threadpriority.belownormal             thpoll.start()         catch ex exception             msgbox(ex.message)         end try     next end sub  public sub invokeuiupdate(test object)     if datagridview1.invokerequired         datagridview1.invoke(new testclassdelegate(addressof uiupdate), test)     else         uiupdate(test)     end if end sub  public sub uiupdate(test object)     thread.sleep(test.index * 100)     datagridview1.rows(test.index).cells(1).value = test.testval1     me.refresh()     thread.sleep(100)     datagridview1.rows(test.index).cells(2).value = test.testval2     me.refresh()     thread.sleep(100)     datagridview1.rows(test.index).cells(3).value = test.testval3     me.refresh()     thread.sleep(100)     datagridview1.rows(test.index).cells(4).value = test.testval4     me.refresh()     thread.sleep(100)     datagridview1.rows(test.index).cells(5).value = test.testval5     me.refresh()     thread.sleep(100)     datagridview1.rows(test.index).cells(6).value = test.testval6     me.refresh()     interlocked.decrement(threadcount)     me.label2.text = threadcount end sub  end class 

run code little bit different, how structure should multithreading in vb.net ( has vb.net not passing namespaces models understand )

this startthread mainthread in load or w/e have you

private sub dosomethingsimple()     dim dosomethingsimple_thread new thread(addressof dosimple)     dosomethingsimple_thread.priority = threadpriority.abovenormal     dosomethingsimple_thread.start(me) end sub 

this actual thread ( new model / class or in same class )

private sub dosimple(beginform form)     'do whatever doing has nothing ui      'for ui calls use following     somethinginvoked(passiblevariable, beginform)  end sub 

write delegate , invoke method each call main thread.

delegate sub somethinginvoked_delegate(s integer, beginform form) sub somethinginvoked_invoke(byval s integer, beginform form)     if beginform.nameofcontrolyouareupdating.invokerequired ' change nameofcontrolyouareupdating name of control on form wish update         dim d new somethinginvoked_delegate(addressof somethinginvoked_invoke)         beginform.invoke(d, new object() {s, beginform})     else          'do something...         beginform.nameofcontrolyouareupdating.condition = parameter      end if end sub 

this tested ( non hanging ) way of writing threads in vb.net

if need further implementing code template let me know :p


Comments