我是VB.NET的初學者,請耐心等待。VB.NET [跨線程操作無效:控制'txtIncomingText'從線程訪問........]
我已經在VB.NET中下載了多客戶端TCP-IP套接字服務器客戶端應用程序。服務器聽得很好,但客戶端遇到以下例外情況:
「跨線程操作無效:控制'txtIncomingText'從其創建線程以外的線程訪問。
如果你們可以幫助我更正版本的代碼,我將會很有幫助。謝謝。
「-------客戶端代碼-------
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Public Class frmClient
Inherits Form
Private Sub frmClient_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private _clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Public Sub New()
InitializeComponent()
End Sub
Private receivedBuf As Byte() = New Byte(1023) {}
Private thr As Thread
Private Sub ReceiveData(ar As IAsyncResult)
Dim socket As Socket = DirectCast(ar.AsyncState, Socket)
Dim received As Integer = socket.EndReceive(ar)
Dim dataBuf As Byte() = New Byte(received - 1) {}
Array.Copy(receivedBuf, dataBuf, received)
' These two lines encounter an error ->>>>>
txtIncomingText.Text = (Encoding.ASCII.GetString(dataBuf))
rbChat.Text = "\nServer: " + txtIncomingText.Text
_clientSocket.BeginReceive(receivedBuf, 0, receivedBuf.Length, SocketFlags.None, New AsyncCallback(AddressOf ReceiveData), _clientSocket)
End Sub
Private Sub SendLoop()
While True
'Console.WriteLine("Enter a request: ");
'string req = Console.ReadLine();
'byte[] buffer = Encoding.ASCII.GetBytes(req);
'_clientSocket.Send(buffer);
Dim receivedBuf As Byte() = New Byte(1023) {}
Dim rev As Integer = _clientSocket.Receive(receivedBuf)
If rev <> 0 Then
Dim data As Byte() = New Byte(rev - 1) {}
Array.Copy(receivedBuf, data, rev)
lbStt.Text = ("Received: " + Encoding.ASCII.GetString(data))
rbChat.AppendText(vbLf & "Server: " + Encoding.ASCII.GetString(data))
Else
_clientSocket.Close()
End If
End While
End Sub
Private Sub LoopConnect()
Dim attempts As Integer = 0
While Not _clientSocket.Connected
Try
attempts += 1
_clientSocket.Connect(IPAddress.Loopback, 420)
Catch generatedExceptionName As SocketException
'Console.Clear();
lbStt.Text = ("Connection attempts: " + attempts.ToString())
End Try
End While
lbStt.Text = ("Connected!")
End Sub
Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
If _clientSocket.Connected Then
Dim buffer As Byte() = Encoding.ASCII.GetBytes(txtText.Text)
_clientSocket.Send(buffer)
rbChat.AppendText("Client: " + txtText.Text)
End If
End Sub
Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
LoopConnect()
' SendLoop();
_clientSocket.BeginReceive(receivedBuf, 0, receivedBuf.Length, SocketFlags.None, New AsyncCallback(AddressOf ReceiveData), _clientSocket) Dim buffer As Byte() = Encoding.ASCII.GetBytes("@@" + txtName.Text)
_clientSocket.Send(buffer)
End Sub
End Class
錯誤信息的哪些部分讓您感到困惑?它非常清楚 - 您無法從非UI線程訪問控件。 – Plutonix
[Cross-thread operation not valid:可以從一個線程以外的線程訪問它的線程](https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control -accessor-from-a-thread-other-the) –
必須有一個vb.net副本。 – djv