3

I hope somebody can help me. I am trying to access a listbox from another thread and the rare thing is that invokerequired is giving me "false", it suppose to be able to access it directly but nothing happens, the item is not added to the listbox.

Here is my code and thanks in advance:

Imports System.Threading
Imports System.Net
Imports System.Net.Sockets

Public Class FrmTCPServer
    Dim fn, temp_file, str_rute, str_filename, str_content, file_name, clNo, NewText As String
    Dim file_len, recfilelen, counter As Integer

    Dim serverSocket As New TcpListener(IPAddress.Any, 9088)
    Dim clientSocket As TcpClient

    Public thread As Thread = Nothing

    Private Sub FrmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Lbconn.Items.Clear()

        Dim IPHost As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName)
        lblStatus.Text = "My IP address is " + IPHost.AddressList(1).ToString()
    End Sub

    Private Sub Btnstart_Click(sender As Object, e As EventArgs) Handles Btnstart.Click
        serverSocket.Start()

        ThreadProcSafe("Server Started")

        thread = New Thread(New ThreadStart(AddressOf listenerThread))
        thread.Start()
    End Sub

    Private Sub listenerThread()
        While (True)
            counter += 1
            clientSocket = serverSocket.AcceptTcpClient()
            ThreadProcSafe("Client No: " & Convert.ToString(counter) & " IP: " & (IPAddress.Parse(CType(clientSocket.Client.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString() & " Started!")

            Dim client1 As New FrmTCPServer
            client1.startClient(clientSocket, Convert.ToString(counter))
        End While
    End Sub

    Public Sub startClient(ByVal clientSocket As TcpClient, ByVal counter As Integer)
        thread = New Thread(New ThreadStart(AddressOf handlerThread))
        thread.Start()
    End Sub

    Private Sub handlerThread()
        ThreadProcSafe("Receiving File... ")
    End Sub

    Sub ThreadProcSafe(item As Object)
        If Lbconn.InvokeRequired Then
            Lbconn.Invoke(Sub() Lbconn.Items.Add(item & " (Invoke)"))
        Else
            Lbconn.Items.Add(item & " (No Invoke)") '**Here pass whith no exception but does not add the item to the listbox**
        End If
    End Sub
End Class
Spikexp
  • 33
  • 4
  • seen [this link](http://stackoverflow.com/questions/17334425/vb-net-delegates-and-invoke-can-somebody-explain-these-to-me), maybe helped to you – Behzad Aug 06 '15 at 19:47
  • Hi, thanks for the info Behzad Khosravifar, however, and maybe i am wrong, there is only info about "invokes", my code works fine, with no exceptions thrown, the only thing is that accessing the listbox directly this way, the item is not added. Hope you can help me a little bit more, thanks – Spikexp Aug 07 '15 at 15:32
  • Now, Your code works, but if your means: "Why `lbconn.InvoleRequired` is False ?" So, you did not call `ThreadProcSafe` from another thread ! – Behzad Aug 07 '15 at 16:55
  • In my code, after i start thread "handlerThread", i call "ThreadProcSafe Sub" to add the message and it is not added – Spikexp Aug 07 '15 at 17:12
  • Oh ... I checked now, data Added to ListBox.Items but that is not shown on GUI !!! – Behzad Aug 07 '15 at 17:32
  • 1
    I find your problem, You create form for every start as again!! when you add item to list , data added to new form witch is not show for you. – Behzad Aug 07 '15 at 17:37

2 Answers2

1

In listenerThread method:

Private Sub listenerThread()
    While (True)
        counter += 1
        clientSocket = serverSocket.AcceptTcpClient()
        ThreadProcSafe("Client No: " & Convert.ToString(counter) & " IP: " & (IPAddress.Parse(CType(clientSocket.Client.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString() & " Started!")

        Dim client1 As New FrmTCPServer ' *** THIS PLACE ***
        client1.startClient(clientSocket, Convert.ToString(counter))
    End While
End Sub

You create new FrmTCPServer form and then call startClient on new object. So you data add in new list no this form which is running!

You should change listenerThread method to this:

Private Sub listenerThread()
    While (True)
        counter += 1
        clientSocket = serverSocket.AcceptTcpClient()
        ThreadProcSafe("Client No: " & Convert.ToString(counter) & " IP: " & (IPAddress.Parse(CType(clientSocket.Client.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString() & " Started!")

        Me.startClient(clientSocket, Convert.ToString(counter))
    End While
End Sub
Behzad
  • 3,502
  • 4
  • 36
  • 63
  • Hi, i ran that code and now i can add items to the listbox but i cannot connect multiple clients to my server at the same time, it is processed one at the time – Spikexp Aug 07 '15 at 18:06
  • Well, your question was "I cannot access listbox from another thread ..." and now solved that. To find a solution for new question you must ask a new question and this time add more details about your app and codes. – Behzad Aug 07 '15 at 19:53
  • For solve your question, is better the Programmers to know what happen in your server codes and clients codes. I said is better ask a new question to this new problem to find solution as soon. – Behzad Aug 07 '15 at 19:56
  • 1
    I solved it creating a new class that handles each client, thank you all for your help, it was very illustrating and thanks for your time and patience Behzad! – Spikexp Aug 07 '15 at 21:23
0

Change ThreadProcSafe method to below codes and try again:

Sub ThreadProcSafe(item As Object)
    If Lbconn.InvokeRequired Then
        Lbconn.Invoke(Sub() Lbconn.Items.Add(item))
    Else
        Lbconn.Items.Add(item)
    End If
End Sub
Behzad
  • 3,502
  • 4
  • 36
  • 63
  • Hi again, i did what you suggested and i still cannot add items directly after call thread "handlerThread", i have posted my whole code so you can have a better view what i am talking about, thanks! – Spikexp Aug 07 '15 at 16:41