Imports?System
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站制作、溫宿網(wǎng)絡(luò)推廣、微信小程序、溫宿網(wǎng)絡(luò)營(yíng)銷、溫宿企業(yè)策劃、溫宿品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供溫宿建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址: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
就是讓你處于這個(gè)線程里時(shí),委托另一個(gè)線程去執(zhí)行一些動(dòng)作
我簡(jiǎn)單舉一個(gè)寫richtextbox的例子:
////////////////////////////////////////////
'創(chuàng)建一個(gè)名為 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
/////////////////////
這樣,你在多線程應(yīng)用時(shí),在其他線程里用txtW(str)來(lái)寫richtextbox,就不會(huì)產(chǎn)生錯(cuò)誤了。不然,直接垮線程寫richtextbox,可能會(huì)出現(xiàn)和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() //啟動(dòng)多線程進(jìn)程
Application.DoEvents
Next
End Sub