我正在创建一个程序,可以等待计算机连接到我的服务器
此代码最初是在控制台中实现的,但我希望以GUI形式实现
MsgBox("Server is ready!", MsgBoxStyle.Information, "ChatApp! | Alert")
server = New TcpListener(ipendpoint)
server.Start()
While True
client = server.AcceptTcpClient
Dim c As New Connection
c.stream = client.GetStream
c.streamr = New StreamReader(c.stream)
c.streamw = New StreamWriter(c.stream)
c.hostname = c.streamr.ReadLine
list.Add(c)
FlatListBox1.AddItem(c.hostname)
ListView1.Items.Add(c.hostname & " is connected")
Dim t As New Threading.Thread(AddressOf ListenToConnection)
t.Start(c)
End While
Me.Show()
将其添加到Form_load之后,但是我的表单不可点击,否则
在我的表单运行时,请帮助我将此代码在后台运行
编辑
我在Google某个地方找到了答案。
' Background work
Task.Factory.StartNew(Function()
' Update UI thread
End Function).ContinueWith(Function(t)
End Function, TaskScheduler.FromCurrentSynchronizationContext())
但我不知道如何将其合并到我的代码中
添加此内容后出现错误
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() TextBox1.Text = "Done")
End If
最佳答案
您的表单不可单击,因为您正在锁定UI线程。
您有一个While..End While
循环,该循环永不退出。
您需要更改此设置以在某个时候退出(UI将在运行此循环时被锁定),或者如果需要多次运行,则将代码从UI线程中移出。
有很多选择
我建议您查看Tasks and Continuations,因为您将获得读取流并与UI线程进行交互的结果(即,将文本放入列表框。)
关于.net - VB.net表格不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006674/