Thành 0 Newbie Poster

I have a server chat use socket and multithreading but i can't show data of client to my textbox, I only see that data when i use "Msgbox", I only take a warning about cross-thread...blah...blah but i think it's not my problem so i dismiss it by code:

System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False

My form have a textbox is txtContent, this is my code, I think i have to use Delegate, SynchronizationContext or RaiseEvent but I tried and not success :(
Please help me!

Option Explicit On
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Server
    Dim clientsList As New Hashtable
    Dim serverSocket As New TcpListener(IPAddress.Parse(""), 8888)
    Dim clientSocket As TcpClient

    Private Sub Server_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
        msg("Chat Server Started ....")
        Dim t As New Thread(AddressOf Main)
    End Sub

    Sub Main()
        While True
            clientSocket = serverSocket.AcceptTcpClient()
            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
            Dim networkStream As NetworkStream = clientSocket.GetStream()
            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
            dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
            clientsList(dataFromClient) = clientSocket
            msg(dataFromClient + " Joined chat room ")

            Dim client As New HandlerClient
        End While


    End Sub
    Private Sub msg(ByVal text As String)
        txtContent.Text = txtContent.Text + text + Environment.NewLine
    End Sub
End Class
Public Class HandlerClient
    Public clientSocket As New TcpClient
    Public Sub StartClient(ByVal clSocket As TcpClient)
        Me.clientSocket = clSocket
        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)

    End Sub

    Private Sub doChat()
        Dim requestCount As Integer
        Dim bytesFrom(10024) As Byte
        Dim dataFromClientOfDoChat As String
        Dim rCount As String
        While True
                Dim networkStream As NetworkStream = clientSocket.GetStream()
                networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                dataFromClientOfDoChat = System.Text.Encoding.ASCII.GetString(bytesFrom)
                dataFromClientOfDoChat = dataFromClientOfDoChat.Substring(0, dataFromClientOfDoChat.IndexOf("$"))

                '  Server.txtListClient.Text = ("From client - " +" : " + dataFromClientOfDoChat) 'This line not work
                   Msgbox("From client - " + " : " + dataFromClientOfDoChat)                      'But this line work

                MsgBox("From client - " + "clNo " + " : " + dataFromClientOfDoChat)
                rCount = Convert.ToString(requestCount)
            Catch ex As Exception
            End Try
        End While
    End Sub
End Class

I attached my source which i based on, maybe useful to test

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.