Mr_Random_Guy
Mr_Random_Guy

Reputation: 113

Blank ListView when calling Invoke

listView is owned by the class Form1. The subroutine anotherThread in a separate class transmission is started in a thread by a subroutine in Form1. Form1 owns another public subroutine addItemsListView, which uses Invoke. When transmission.anotherThread calls addItemsListView, the subroutine runs, but listView remains blank.

Have tried delegates, invokes etc. inside each class but the same problem.

Class Form1
    Property myTransmission = New transmission
    Private Sub aSubRoutine() Handles MyBase.Load
        Dim t As New Threading.Thread(
            Sub()
                myTransmission.anotherThread()
            End Sub
        )
        t.Start()
    End Sub

    Public Sub addItemsListView(ByVal items As String())
        If listView.InvokeRequired Then
            listView.Invoke(Sub() addItemsListView(items))
        Else
            For each item In Items
                listView.Items.Add(item)
            Next
        End If
    End Sub
End Class

Class transmission
    Public Sub anotherThread()
        Form1.addItemsListView(New String() {"abc", "def"})
    End Sub
End Class

So I expect "abc" and "def" to be in the listView but it remains completely blank. If I step through the code, however, everything seems to be running smoothly.

Upvotes: 1

Views: 35

Answers (1)

LarsTech
LarsTech

Reputation: 81620

You aren't talking to your existing form. Pass it as a reference:

Public Sub anotherThread(inForm As Form1)
  inForm.addItemsListView(New String() {"abc", "def"})
End Sub

then include your form when you call it:

myTransmission.anotherThread(Me)

Upvotes: 1

Related Questions