Imports System
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、成都网站制作、温宿网络推广、微信小程序、温宿网络营销、温宿企业策划、温宿品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供温宿建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
Imports System.Threading
Public Class Form1
Dim TestThread1, TestThread2 As Thread
Public Sub TestMethod1()
Dim i As Integer
i = 0
While (i 1000)
Label1.Text = i
i += 1
End While
End Sub
Public Sub TestMethod2()
Dim i As Integer
i = 0
While (i 1000)
Label2.Text = i
i += 1
End While
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Control.CheckForIllegalCrossThreadCalls = False
TestThread1 = New Thread(New ThreadStart(AddressOf TestMethod1))
TestThread1.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Control.CheckForIllegalCrossThreadCalls = False
TestThread2 = New Thread(New ThreadStart(AddressOf TestMethod2))
TestThread2.Start()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Control.CheckForIllegalCrossThreadCalls = False
TestThread1 = New Thread(New ThreadStart(AddressOf TestMethod1))
TestThread2 = New Thread(New ThreadStart(AddressOf TestMethod2))
TestThread1.Start()
TestThread2.Start()
End Sub
End Class
委托,Delegate
就是让你处于这个线程里时,委托另一个线程去执行一些动作
我简单举一个写richtextbox的例子:
////////////////////////////////////////////
'创建一个名为 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal txt As String)
'写信息到富文本主窗口
Private Sub txtW(ByVal txt As String)
Dim msgd As New MySubDelegate(AddressOf Me.txtW1)
Dim arg(0) As Object
arg(0) = txt
Me.Invoke(msgd, arg)
End Sub
'委托指向
Private Sub txtW1(ByVal txt As String)
Me.RichTextBox1.AppendText(txt)
End Sub
/////////////////////
这样,你在多线程应用时,在其他线程里用txtW(str)来写richtextbox,就不会产生错误了。不然,直接垮线程写richtextbox,可能会出现和UI线程的冲突。
Sub Main()
Dim thr As Thread
For Pi As Integer=0 To 4 //启用5线程
MulParams =Pi vbTab sFile vbTab dFile vbTab 1 vbTab DelN vbTab cr vbTab cg vbTab cb vbTab IndexI
GlobalParamas(pi)=MulParams .Split(vbTab)
thr=New Thread(AddressOf MyMulThreadCaller)
thr.Start() //启动多线程进程
Application.DoEvents
Next
End Sub