When writing VB.net, it is not that easy to manually change the information in the listview control on the form in the thread. Because Microsoft's framework does not encourage direct access to controls on the form in the thread. This will change the normal execution of the UI process. If this is required, you need to use the invoke function or the invokebegin function in the thread. The following is an example:
Imports system. threading
Public class form1
Delegate sub addlistitem (byval ipstring as string, byval scanport as integer)
Public mydelegate as addlistitem
Public openportcount as integer = 0
Private sub form1_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
Mydelegate = new addlistitem (addressof addlistitemmethod)
End sub
Private sub start_button_click (byval sender as system. Object, byval e as system. eventargs) handles start_button.click
Dim mythread as thread
Mythread = new thread (New threadstart (addressof threadfunction ))
Mythread. Start ()
End sub
Private sub threadfunction ()
Dim mythread as thread
Mythread = new thread (New threadstart (addressof doscanthread ))
Mythread. Start ()
End sub 'threadfunction
Private sub doscanthread ()
Dim mythreadclassobject as new scanthreadclass (me)
Mythreadclassobject. Run ()
End sub
Public sub addlistitemmethod (byval ipstring as string, byval scanport as integer)
Listview_result.items.add (ipstring, openportcount) 'scanip. tostring (), 0)
Listview_result.items (openportcount). subitems. Add (scanport. tostring ())
Openportcount + = 1
End sub 'addlistitemmethod
End Class
Public class scanthreadclass
Private myformcontrol1 as form1
Public sub new (byval myform as form1)
Myformcontrol1 = myform
End sub 'new
Public sub run ()
Myformcontrol1.invoke (myformcontrol1.mydelegate, new object () {"11", 123 })
End sub
End Class
The main note is the call of invoke and the definition of delegate.