Dim th as System.Threading.Thread
th = New System.Threading.Thread(Addressof 对象名(或类名).方法)
对象名(或是类名).属性 = 值 '通过这样可以实现线程传参
.......
th.start()
Dim th as System.Threading.Thread
th = New System.Threading.Thread(Addressof 方法名1)
th.start()
Delegate Sub DelegateHandler(ByVal 参数 as String) '声明一个委托
private Sub 方法名1()
dim dh as DelegateHandler = New DelegateHandler(AddressOf 方法名2)
me.invoke(dh, new Object()...{"value"})
End Sub
private Sub 方法名2(ByVal str as String)
'被委托给的方法 ,处理委托后的数据
End Sub
'声明一个线程
Dim t As System.Threading.Thread
t = New Thread(AddressOf WaitData)
t.Start()
Dim s As Socket
Public Sub WaitData()Sub WaitData()
s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim localEndPoint As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 1024)
s.Bind(localEndPoint)
s.Listen(100)
Me.dealData(s)
End Sub

Private Sub DealData()Sub DealData(ByRef sock As Socket)
While (True)
Dim bytes As Byte() = New Byte(1024) {}
Dim ss As Socket = s.Accept()
i = ss.Receive(bytes)
Dim valstr As String = Encoding.Default.GetString(bytes, 0, i)
'处理接收到的数据
'见第二步线程与委托技术
'发送回数据
ss.Send(Encoding.Default.GetBytes("已收到!!Love you")) '发回数据
End While
End Sub
Try
Dim bytes As Byte() = New Byte(1024) {}
s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim localEndPoint As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 1024)
s.Connect(localEndPoint)
s.Send(Encoding.Default.GetBytes("要发送的数据"))
s.Receive(bytes)
'处理接收到的数据
Catch ex As Exception
Finally
s.Close()
End Try
Delegate Sub DelegateHandler()Sub DelegateHandler(ByVal str As String) '声明一个委托
Dim dh As DelegateHandler = New DelegateHandler(AddressOf 方法名) 'New 出一个委托并指定委托方法
Me.invoke(dh, New Object(){"传递的参数"}) '调用invoke方法

Private Sub 方法名()Sub 方法名(ByVal str As String)
'处理委托的数据str
End Sub| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|