gpt4 book ai didi

vb.net - 我无法从另一个线程访问列表框,并且 vb.net 中不需要调用

转载 作者:行者123 更新时间:2023-12-02 04:24:01 24 4
gpt4 key购买 nike

我希望有人能帮助我。我试图从另一个线程访问列表框,罕见的是,invokerequired 给了我“false”,它应该能够直接访问它,但没有任何反应,该项目没有添加到 listbox .

这是我的代码,提前致谢:

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

最佳答案

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

Dim client1 As New FrmTCPServer ' *** THIS PLACE ***
client1.startClient(clientSocket, Convert.ToString(counter))
End While
End Sub

您创建新的FrmTCPServer 表单,然后在新对象上调用startClient。因此,您将数据添加到新列表中,而不是正在运行的表单!

您应该将 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

关于vb.net - 我无法从另一个线程访问列表框,并且 vb.net 中不需要调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31864232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com