0

Basically, the title says it all; im havin a problem in adding my incoming connections to my listview. (I have the addclient function in a separate class that adds a new client to the listview, on accept.

Here is the function's code:

     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

Thanks in advance!

  • What sort of trouble are you having? Do you get an error message? – ChrisGPT was on strike Mar 18 '14 at 20:56
  • He at least should get a stack overflow error, because the function is calling itself infinitely when invoke is required. But like Chris said: What exactly is your problem? – Jens Mar 18 '14 at 21:03
  • Standard vb.net problem, Form1.InvokeRequired is *False*, even though you are calling this code from another thread. You have to stop buying into the notion that a *type name* like Form1 can have an *instance* property like InvokeRequired. OOP school visit required, writing C# code for a while can throw the mental switch. Takes between 6 months and a year, or never. – Hans Passant Mar 18 '14 at 22:21

1 Answers1

0

Your method needs an Else block to the If statement. You're doing this:

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

'Update the UI here.

when what you should be doing is this:

If someControl.InvokeRequired Then
    someControl.Invoke(someDelegate)
Else
    'Update the UI here.
End If
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46