VB.NET如何从另一个类的线程更改文本框?

时间:2015-01-28 09:04:11

标签: vb.net multithreading

我搜索谷歌非常多次,我知道我的问题很老但我仍然无法从另一个班级的线程中更改我的文本框,没有错误且没有任何文本框更改,请帮助我向文本框

显示dataFromClientOfDoChat

我的问题在第71-72行(我在靠近底部评论的那一行)
我的代码有一个文本框是txtContent

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
    Dim clNo As String

    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 ")

            clNo = dataFromClient
            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.txtContent.Text = ("From client - " + "clNo " + " : " + dataFromClientOfDoChat) ' this line not work

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

0 个答案:

没有答案