vb.net - Multi-Threading IP Address Pings Causing Application Crash -


boy, learning new can real headache if can't find solid source. have been designing applications in linear fashion time , want step more powerful approach. have been reading on threading, , perhaps have gone larger level should. however, 1 steps when application calls , no better time present learn new.

my program designed seems rather simple, has become extremely difficult create in smooth running manor. original design created object of each device on network wished ping, in real world environment kindles. goal ensure still connected network pining them. used for loop , obj array set on timer. had unexpected results causing listview flicker , load after listview1.items.clear. evolved updating list items rather clearing them , flicker remained.

i assumed due slow process of array , pings started hunting solutions , came across multi-threading. have known time, have yet dive practice. program seemed need more speed , smoother operation took stab @ it. below code in complete form result, crashes , throws errors. have not used threading intended. using in simpler functions works fine , feel have grasp. if want program pointlessly run counters.

i don't know next in steps getting task done, , figure combining several different methods mush of dead program. use getting on track this. comments welcome , thank checking out code.

form1 code

public class form1  'obj array public shared objdevice new list(of kdevice) 'thread array each obj public shared thread() system.threading.thread  private sub iprefresh(objid, itempos)      dim objdev kdevice = objid      if my.computer.network.ping(objdev.kip)         objdev.kstatus = "online"         objdev.kpings = 0      else         objdev.kpings += 1     end if      if objdev.kpings >= 8         objdev.kstatus = "offline"         objdev.kpings = 0         listview1.items(itempos).backcolor = color.red     end if      dim str(4) string     dim itm listviewitem     str(0) = objdev.kname     str(1) = objdev.kip     str(2) = objdev.kstatus     str(3) = objdev.kpings     itm = new listviewitem(str)     listview1.items(itempos) = itm  end sub  private sub form1_load(sender object, e eventargs) handles mybase.load      me.checkforillegalcrossthreadcalls = false      ' adding listview columns     listview1.columns.add("device", 100, horizontalalignment.left)     listview1.columns.add("ip address", 150, horizontalalignment.left)     listview1.columns.add("status", 60, horizontalalignment.left)     listview1.columns.add("pings", 60, horizontalalignment.left)      dim iplist new list(of string)     dim namelist new list(of string)      using myreader new microsoft.visualbasic.fileio.textfieldparser("kdevices.csv")         myreader.textfieldtype = microsoft.visualbasic.fileio.fieldtype.delimited         myreader.delimiters = new string() {","}         dim currentrow string()         dim rowp integer = 1         while not myreader.endofdata             try                 currentrow = myreader.readfields()                 dim cellp integer = 0                 dim ntemp string = ""                  each currentfield string in currentrow                      select case cellp                          case 0                             namelist.add(currentfield.replace("""", ""))                         case 1                             iplist.add(currentfield.replace("""", ""))                      end select                     cellp += 1                  next             catch ex microsoft.visualbasic.fileio.malformedlineexception                 msgbox("line " & ex.message & " invalid.  skipping")             end try             rowp += 1         end while     end using      dim namelar string() = namelist.toarray     dim iplar string() = iplist.toarray      redim preserve thread(namelar.length)      integer = 0 namelar.length - 1          dim newdevice new kdevice         dim objnum =          objdevice.add(newdevice)          newdevice.kname = namelar(i)         newdevice.kip = iplar(i)          if my.computer.network.ping(newdevice.kip)             newdevice.kstatus = "online"         else             newdevice.kstatus = "loading"         end if          dim str(4) string         dim itm listviewitem         str(0) = newdevice.kname         str(1) = newdevice.kip         str(2) = newdevice.kstatus         str(3) = newdevice.kpings         itm = new listviewitem(str)         if newdevice.kstatus = "loading"             itm.backcolor = color.yellow         end if         listview1.items.add(itm)          thread(objnum) = new system.threading.thread(sub() me.iprefresh(objdevice(objnum), objnum))      next  end sub  private sub timer1_tick(sender object, e eventargs) handles timer1.tick      integer = 0 objdevice.count - 1          thread(i).start()      next  end sub  end class 

kdevice class

public class kdevice  private strkname string private strkip string private strkstatus string private strklaststatus string private strkpings integer = 0  public property kname string              return strkname     end      set(value string)         strkname = value     end set  end property  public property kip string              return strkip     end      set(value string)         strkip = value     end set  end property  public property kstatus string              return strkstatus     end      set(value string)         strkstatus = value     end set  end property  public property kpings integer              return strkpings     end      set(value integer)         strkpings = value     end set  end property  end class 

the error / crash on line 32 of code when tries pass update listview item

an unhandled exception of type 'system.argumentexception' occurred in microsoft.visualbasic.dll  additional information: invalidargument=value of '18' not valid 'index'. 

or

an unhandled exception of type 'system.nullreferenceexception'  occurred in microsoft.visualbasic.dll  additional information: object reference not set instance of object. 

if code not make sense, or @ lease idea of trying make do, please let me know , explain whichever parts unclear. again thank looking on issue.

just possible issue noticed:

dim str(4) string     dim itm listviewitem     str(0) = newdevice.kname     str(1) = newdevice.kip     str(2) = newdevice.kstatus     str(3) = newdevice.kpings     itm = new listviewitem(str)     if newdevice.kstatus = "loading"         itm.backcolor = color.yellow     end if     listview1.items.add(itm) 

in bit here, declare str(4) 5 possible indexes (remember starts @ zero), should have 4 (str(3)) . don't think whole issue, small bit should fix. may want other ways update listview without setting

me.checkforillegalcrossthreadcalls = false 

here's awesome guide helped me when did first multi threaded application: http://checktechno.blogspot.com/2012/11/multi-thread-for-newbies.html


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -