vb.net多线程简单示例
主线程中的代码
Dim delegate1 As B3D4D69AF0E1DB8A7B7EE6FDelegate
delegate1 = AddressOf B3D4D69AF0E1DB8A7B7EE6F
Dim ar As System.IAsyncResult
Dim param(2) As Object
param(0) = Me
param(1) = delegate1
ar = delegate1.BeginInvoke(resStr, CallBackB3D4D69AF0E1DB8A7B7EE6FDelegate, param)
delegate1 = AddressOf B3D4D69AF0E1DB8A7B7EE6F
Dim ar As System.IAsyncResult
Dim param(2) As Object
param(0) = Me
param(1) = delegate1
ar = delegate1.BeginInvoke(resStr, CallBackB3D4D69AF0E1DB8A7B7EE6FDelegate, param)
耗时的操作代码
Private Function B3D4D69AF0E1DB8A7B7EE6F(ByVal D7B89BE7F84108A1EF3ED31124D049() As String) As DataSet
Dim result As DataSet = Nothing
Try
Dim ws As New SRL.F408C4CE284F489C7B22E2461FE4BFSoapClient()
result = ws.E13F1B91BD3546FA93DC9F0B8CD56B7B(D7B89BE7F84108A1EF3ED31124D049) ‘web service访问,此操作很耗时
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return result
End Function
Delegate Function B3D4D69AF0E1DB8A7B7EE6FDelegate(ByVal D7B89BE7F84108A1EF3ED31124D049() As String) As DataSet ‘一定要定义,否则会出错
Dim result As DataSet = Nothing
Try
Dim ws As New SRL.F408C4CE284F489C7B22E2461FE4BFSoapClient()
result = ws.E13F1B91BD3546FA93DC9F0B8CD56B7B(D7B89BE7F84108A1EF3ED31124D049) ‘web service访问,此操作很耗时
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return result
End Function
Delegate Function B3D4D69AF0E1DB8A7B7EE6FDelegate(ByVal D7B89BE7F84108A1EF3ED31124D049() As String) As DataSet ‘一定要定义,否则会出错
callback处理,上面的函数是返回dataset,所以这里线程结束后也是获取dataset
Private CallBackB3D4D69AF0E1DB8A7B7EE6FDelegate As AsyncCallback = AddressOf CallBackB3D4D69AF0E1DB8A7B7EE6F
Sub CallBackB3D4D69AF0E1DB8A7B7EE6F(ByVal ar As IAsyncResult)
Try
Dim delegate1 As B3D4D69AF0E1DB8A7B7EE6FDelegate = ar.AsyncState(1)
Dim r As DataSet = delegate1.EndInvoke(ar) '获取ws结果,然后操作这个结果
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub CallBackB3D4D69AF0E1DB8A7B7EE6F(ByVal ar As IAsyncResult)
Try
Dim delegate1 As B3D4D69AF0E1DB8A7B7EE6FDelegate = ar.AsyncState(1)
Dim r As DataSet = delegate1.EndInvoke(ar) '获取ws结果,然后操作这个结果
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub