I cannot access the list from another thread and no call is required in vb.net
Hope someone can help me. I'm trying to access a list from another thread and the rare thing is that invokerequired gives me "false", it should be able to access it directly, but nothing happens, no item is added to 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
+3
source to share
2 answers
Into 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 a new form FrmTCPServer
and then call startClient
on a new object. This way, you don't add data to a new list without this form!
You should change the way listenerThread
:
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
+1
source to share