首页 > 解决方案 > 调用 Invoke 时出现空白 ListView

问题描述

listView归类所有Form1。一个单独的类传输中的子程序anotherThread由一个线程中的子程序启动Form1Form1拥有另一个公共子例程addItemsListView,它使用Invoke. 当transmission.anotherThread调用addItemsListView时,子例程运行,但listView保持空白。

在每个类中都尝试过委托、调用等,但同样的问题。

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

所以我希望“abc”和“def”在listView但它仍然完全空白。但是,如果我单步执行代码,一切似乎都运行顺利。

标签: vb.netmultithreadingclasslistview

解决方案


你不是在和你现有的表格说话。将其作为参考传递:

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

然后在您调用它时包含您的表单:

myTransmission.anotherThread(Me)

推荐阅读