概述
[原帖地址]http://topic.csdn.net/u/20111019/17/ac098c48-ed25-4e1f-acd0-ec192ca44eac.html
'原先错误的写法!
-------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyThread As New System.Threading.Thread(AddressOf sum)
MyThread.Start()
End Sub
Private Sub sum()
Dim i, sum As Integer
For i = 0 To 100
sum += i
Next
Me.Text = CStr(sum)
'在这里就会报错,教我"如何跨线程调用windows窗体控件",表示没看懂
End Sub
-------------------------------------
'正确的线程委托写法
-------------------------------------
Private Delegate Sub myDelegate(ByVal myString As String) '自定义一个委托
Private Sub SetWindowText(ByVal myString As String) '与委托相同签名的函数或过程
Me.Text = myString
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyThread As New System.Threading.Thread(AddressOf sum)
MyThread.Start()
End Sub
Private Sub sum()
Dim i, sum As Integer
For i = 0 To 100
sum += i
Next
Me.Invoke(New myDelegate(AddressOf SetWindowText), sum.ToString) '委托的调用
End Sub
最后
以上就是超级小懒虫为你收集整理的vb.net中线程委托的简单实例的全部内容,希望文章能够帮你解决vb.net中线程委托的简单实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复