12 Replies - 3082 Views - Last Post: 28 January 2015 - 07:24 AM Rate Topic: -----

#1 thanhmssl10   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 27-January 15

How to change textbox.text from a thread of another class?

Posted 27 January 2015 - 11:51 AM

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("127.0.0.1"), 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
            serverSocket.Start()
            msg("Chat Server Started ....")
            Dim t As New Thread(AddressOf Main)
            t.Start()
        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
                client.StartClient(clientSocket)
            End While
    
            clientSocket.Close()
            serverSocket.Stop()
            msg("exit")
    
        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)
            ctThread.Start()
    
        End Sub
    
        Private Sub doChat()
            Dim requestCount As Integer
            Dim bytesFrom(10024) As Byte
            Dim dataFromClientOfDoChat As String
            Dim rCount As String
            While True
                Try
                    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
                    MsgBox(ex.ToString)
                End Try
            End While
        End Sub
    End Class



I attached source code I based on, maybe useful to test

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: How to change textbox.text from a thread of another class?

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,865
  • Joined: 01-February 13

Re: How to change textbox.text from a thread of another class?

Posted 27 January 2015 - 12:14 PM

Hi,

Could you show us what line or lines it is that you are trying to set the TextBox`s text that is throwing the error.

And yes, you can use a Delegate sub to set the text of a control from another thread.

This post has been edited by IronRazer: 27 January 2015 - 12:14 PM

Was This Post Helpful? 1
  • +
  • -

#3 thanhmssl10   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 27-January 15

Re: How to change textbox.text from a thread of another class?

Posted 27 January 2015 - 12:17 PM

Oh so fast :)), thank for reply, at line 72 i commented

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


Was This Post Helpful? 0
  • +
  • -

#4 thanhmssl10   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 27-January 15

Re: How to change textbox.text from a thread of another class?

Posted 27 January 2015 - 12:27 PM

Oh,have a confusion, my form have 2 textbox, txtListClient and txtContent, but i think it's not important,
The most important is how to show
dataFromClientOfDoChat
to a textbox of MainForm (in my project is Server)
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,865
  • Joined: 01-February 13

Re: How to change textbox.text from a thread of another class?

Posted 27 January 2015 - 12:31 PM

We don`t mess around. LOL :D/>

Anyways, you can change the code in your DoChat sub like this to invoke a new Delegate Sub.
                '  Server.txtListClient.Text = ("From client - " +" : " + dataFromClientOfDoChat) 'This line not work
                'Msgbox("From client - " + " : " + dataFromClientOfDoChat)                      'But this line work
                Dim StrMessage As String = "From client - " & " : " & dataFromClientOfDoChat
                Invoke(New SetTextDel(AddressOf SetText), StrMessage)



Then you need to add the Delegate Sub and the Sub that will set the text like this.
    Private Delegate Sub SetTextDel(ByVal msg As String)

    Private Sub SetText(ByVal msg As String)
        TextBox1.Text = msg
    End Sub



Also, when you join strings together you should use the "&" character, not the "+" character. That will cause you headaches eventually.

This post has been edited by IronRazer: 27 January 2015 - 12:33 PM

Was This Post Helpful? 1
  • +
  • -

#6 thanhmssl10   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 27-January 15

Re: How to change textbox.text from a thread of another class?

Posted 27 January 2015 - 12:37 PM

I gonna try it right now, don't go away please! :chinese:

I think I have a problem with this line:
Invoke(New SetTextDel(AddressOf SetText), StrMessage)

Name 'Invoke' is not declared
Was This Post Helpful? 0
  • +
  • -

#7 thanhmssl10   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 27-January 15

Re: How to change textbox.text from a thread of another class?

Posted 28 January 2015 - 04:56 AM

I tried use RaiseEvent and NEARLY success.
I can Raise a Event from class HandlerClient (with datafromclient) to Server form and show data in textbox But:
When second client connect to Server, Data of First client not show in textbox
when third client connect to server, Data of First client and Second client not show in textbox
.....

Only one thread can RaiseEvent! :unsure:
All my project below (VS2008), please help me :helpsmilie:
Attached File  MyProject.zip (175.83K)
Number of downloads: 130
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,865
  • Joined: 01-February 13

Re: How to change textbox.text from a thread of another class?

Posted 28 January 2015 - 05:19 AM

You have caught me out of my element there. I never dealt with servers or communicating between computers like that.

I am going to have to let others that have that experience under their belt answer that. There may be a whole better way to go about doing this which i would not know the first thing about.

I only have experience with sending data between threads. Sorry i don`t have an answer for that, i wish i did. :(
Was This Post Helpful? 0
  • +
  • -

#9 thanhmssl10   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 27-January 15

Re: How to change textbox.text from a thread of another class?

Posted 28 January 2015 - 05:22 AM

Okay, so I will waiting somebody help me :)
Was This Post Helpful? 1
  • +
  • -

#10 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,865
  • Joined: 01-February 13

Re: How to change textbox.text from a thread of another class?

Posted 28 January 2015 - 05:44 AM

Hi,

I found these links that you can check out if you have not already. I just figured that maybe they would be of some help. They all have chat example projects that you can download and take a look through the full code to see how they are doing this.

They are all for VB.Net so, no converting from another language is needed. I know i have learned a lot from downloading projects from CodeProject and studying their code.

A TCP/IP Chat Program

Chatting Application Using DotNet

VB.NET Chat Server Program
Was This Post Helpful? 1
  • +
  • -

#11 thanhmssl10   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 27-January 15

Re: How to change textbox.text from a thread of another class?

Posted 28 January 2015 - 05:49 AM

thank you, i will try it
Was This Post Helpful? 0
  • +
  • -

#12 deery5000   User is offline

  • D.I.C Lover

Reputation: 87
  • View blog
  • Posts: 1,097
  • Joined: 09-May 09

Re: How to change textbox.text from a thread of another class?

Posted 28 January 2015 - 06:55 AM

I raised a thread on this recently

I was trying to invoke the main thread using a thread in another class. I couldn't get this to work at all, I don't know if this was a defect in VS or by design. I used delegate and other means with no joy.

However what I did notice was, the code I used to invoke the main thread sat in the forms class it worked without issue but if I moved the code to a separate class is did not.

check out my thread here, theres a test sample of the issue

http://www.dreaminco...y-using-thread/
Was This Post Helpful? 1
  • +
  • -

#13 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,865
  • Joined: 01-February 13

Re: How to change textbox.text from a thread of another class?

Posted 28 January 2015 - 07:24 AM

Quote

check out my thread here, theres a test sample of the issue


I forgot about seeing that thread not long ago. _HAWK_ had a good tip about not needing to use a Delegate if you have .net 4.0 or 4.5 too. I always forget about that being i am on a old XP machine and only using .Net 3.5.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1