Sub Main()
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的青羊網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
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() //啟動多線程進(jìn)程
Application.DoEvents
Next
End Sub
Imports?System
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
就是讓你處于這個線程里時,委托另一個線程去執(zhí)行一些動作
我簡單舉一個寫richtextbox的例子:
////////////////////////////////////////////
'創(chuàng)建一個名為 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)用時,在其他線程里用txtW(str)來寫richtextbox,就不會產(chǎn)生錯誤了。不然,直接垮線程寫richtextbox,可能會出現(xiàn)和UI線程的沖突。
Public?Class?Form1
Public?Class?SquareClass?'把多線程調(diào)用的函數(shù)封裝到類中,通過類事件返回
Public?Value?As?Double
Public?Square?As?Double
Public?Event?ThreadComplete(ByVal?Square?As?Double)
Public?Sub?CalcSquare()
Square?=?Value?*?Value
RaiseEvent?ThreadComplete(Square)
End?Sub
End?Class
Dim?WithEvents?oSquare?As?SquareClass
Private?Sub?Button1_Click(sender?As?Object,?e?As?EventArgs)?Handles?Button1.Click?'多線程返回值測試,當(dāng)線程運(yùn)行完成激發(fā)事件
oSquare?=?New?SquareClass()
Dim?t?As?New?Threading.Thread(AddressOf?oSquare.CalcSquare)
oSquare.Value?=?30
t.Start()
End?Sub
Sub?SquareEventHandler(ByVal?Square?As?Double)?Handles?oSquare.ThreadComplete?'響應(yīng)事件函數(shù)
MsgBox("The?square?is?"??Square)
End?Sub
End?Class