vb.net如何跨线程调用窗体控件
自己写了几行代码,在调试过程中说是线程间操作无效,查询之后发现是要先对线程进行委托,求教要如何进行改动。代码如下:
Private Sub Listen()
Try
tlTcpListen = New TcpListener(IPAddress.Parse("59.77.43.240"), iPort) '以定义的端口号来初始化TcpListener实例
tlTcpListen.Start() '开始监听
ToolStripStatusLabel1.Text = "正在监听..."
skSocket = tlTcpListen.AcceptSocket() '通过TCP连接请求
nsStream = tcClient.GetStream() '用以发送、接收数据的网络基础数据流
srRead = New StreamReader(nsStream) '以得到的网路基础数据流来初始化StreamReader实例
ToolStripStatusLabel1.Text = "已经建立TCP连接!"
'循环侦听
While blistener
Dim sMessage As String = srRead.ReadLine() '从网络基础数据流中读取一行数据
If (sMessage = "STOP") Then
tlTcpListen.Stop() '关闭侦听
nsStream.Close()
srRead.Close() '释放资源
ToolStripStatusLabel1.Text = "无连接!"
thThreadRead.Abort() '中止线程
Return
Else
'判断是否为断开TCP连接控制码
Dim sTime As String = DateTime.Now.ToShortTimeString()
'获取接收数据的时间
ListBox1.Items.Add(sTime + " " + sMessage)
End If
End While
Catch ex As System.Security.SecurityException
MessageBox.Show("侦听失败!", "错误")
End Try
End Sub