When writing vb.net, it is not so easy to find the information in the ListView control that you want to thread on the form to be changed online. Because Microsoft's framework does not encourage online thread to directly access controls on the form. Because this will change the normal execution of the UI process. If you must do this, you need to use the Invoke function or the Invokebegin function in the thread. Here 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 thing to note is the invocation of invoke and the definition of delegate.