Public?Class?Form1
創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)東鄉(xiāng),10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
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?'多線程返回值測(cè)試,當(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
委托,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
多線程一般是不推薦用的,因?yàn)榫€程之間如果有共享資源的話會(huì)引起競(jìng)爭(zhēng),需要加鎖處理;而且線程間沒有時(shí)序關(guān)系,所以你在調(diào)試中可能會(huì)出現(xiàn)異步處理結(jié)束順序與開始處理順序不一致的情況(我在調(diào)試中已經(jīng)發(fā)現(xiàn)該問題)。
針對(duì)你提出的這個(gè)問題,采用了多線程處理,利用的是BackgroundWorker也就是異步處理控件進(jìn)行了處理。
代碼已經(jīng)經(jīng)過調(diào)試通過。歡迎交流,如有問題,留下QQ或其他聯(lián)系方式。
代碼如下,并附程序截圖。
‘---------------------------------------------------
Imports?System.ComponentModel?'導(dǎo)入異步控件命名空間
Public?Class?Form1
Private?howmany?As?Integer?=?10
Private?AnalysisNumber(0?To?howmany?-?1)?As?BackgroundWorker
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
ListBox1.Items.Clear()
creatNewBackgroundWorker()
addHandle()
startWork()
End?Sub
Private?Sub?creatNewBackgroundWorker()
For?i?As?Integer?=?0?To?AnalysisNumber.Length?-?1
AnalysisNumber(i)?=?New?BackgroundWorker
Next
End?Sub
Private?Sub?addHandle()
For?i?As?Integer?=?0?To?AnalysisNumber.Length?-?1
AddHandler?AnalysisNumber(i).DoWork,?AddressOf?AnalysisNumber_DoWork
AddHandler?AnalysisNumber(i).RunWorkerCompleted,?AddressOf?AnalysisNumber_RunWorkerCompleted
Next
End?Sub
Private?Sub?startWork()
For?i?As?Integer?=?0?To?9
Dim?temp(0?To?9)?As?Integer
For?j?As?Integer?=?1?To?10
temp(j?-?1)?=?10?*?i?+?j
Next
AnalysisNumber(i).RunWorkerAsync(temp)
Next
End?Sub
Private?Sub?AnalysisNumber_DoWork(ByVal?sender?As?Object,?ByVal?e?As?System.ComponentModel.DoWorkEventArgs)
Dim?data?As?Integer()
data?=?CType(e.Argument,?Integer())
Dim?temp?As?Integer
For?i?As?Integer?=?0?To?data.Length?-?1
temp?=?data(i)
data(i)?=?temp?*?temp
Next
e.Result?=?data
End?Sub
Private?Sub?AnalysisNumber_RunWorkerCompleted(ByVal?sender?As?Object,?ByVal?e?As?System.ComponentModel.RunWorkerCompletedEventArgs)
Dim?data?As?Integer()
data?=?CType(e.Result,?Integer())
For?i?As?Integer?=?0?To?data.Length?-?1
ListBox1.Items.Add(data(i))
Next
End?Sub
End?Class
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
定義:
Dim g_ReceiveThread As System.Threading.Thread
創(chuàng)建:
g_ReceiveThread = New System.Threading.Thread(AddressOf ReceiveProc)
開始:
g_ReceiveThread.Start()
結(jié)束:
Dim waitStart As Long = My.Computer.Clock.TickCount + 1000 ‘超時(shí)
While (g_ReceiveThread.ThreadState System.Threading.ThreadState.Stopped)
If My.Computer.Clock.TickCount = waitStart Then
g_ReceiveThread.Abort()
End If
Application.DoEvents()
End While