无法从其他线程向listview添加项目

时间:2014-03-18 20:38:32

标签: vb.net multithreading listview invoke

基本上,标题说明了一切;我有一个问题,我的传入连接添加到我的列表视图。 (我将addclient函数放在一个单独的类中,该类将新的客户端添加到listview中,接受。

这是函数的代码:

     Delegate Sub _AddClient(ByVal Client As Socket)
    Public Sub AddClient(ByVal Client As Socket)
        Dim DummyString As String = ""
        Dim IPEP As String() = Client.LocalEndPoint.ToString.Split(":")
        Dim lvi As New ListViewItem(IPEP(0))


        lvi.Tag = Client
        lvi.SubItems.Add(IPEP(1))
        lvi.SubItems.Add(DummyString)
        lvi.SubItems.Add(DummyString)
        lvi.SubItems.Add(DummyString)
        lvi.SubItems.Add(DummyString)
        lvi.SubItems.Add(DummyString)
        lvi.SubItems.Add(DummyString)
        lvi.SubItems.Add(DummyString)
        lvi.SubItems.Add(DummyString)
        lvi.SubItems.Add(DummyString)
        If Form1.AeroListView1.InvokeRequired Then
            Form1.AeroListView1.Invoke(New _AddClient(AddressOf AddClient), Client)
            Exit Sub
        Else
            Form1.AeroListView1.Items.Add(lvi)
        End If

    End Sub

提前致谢!

1 个答案:

答案 0 :(得分:0)

您的方法需要对If语句使用Else块。你这样做:

If someControl.InvokeRequired Then
    someControl.Invoke(someDelegate)
End If

'Update the UI here.

当你应该做的是这样的:

If someControl.InvokeRequired Then
    someControl.Invoke(someDelegate)
Else
    'Update the UI here.
End If