Sub Main() Dim thr As New Thread(AddressOf 循環(huán)) thr.Start("a") End Sub Sub 循環(huán)(a() As String) '這里隨你干什么循環(huán)也行 For Each i As String In a MsgBox(i) Next End Sub
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供奈曼網(wǎng)站建設、奈曼做網(wǎng)站、奈曼網(wǎng)站設計、奈曼網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、奈曼企業(yè)網(wǎng)站模板建站服務,10多年奈曼做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
ListBox操作需要用Invoke。另外多線程方式并不是并行處理。
建議采用點虐 framework 4 帶的并行處理循環(huán)操作,若不是4.0版本的framework,可以使用Aforge的并行庫比較方便。
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
多線程一般是不推薦用的,因為線程之間如果有共享資源的話會引起競爭,需要加鎖處理;而且線程間沒有時序關系,所以你在調試中可能會出現(xiàn)異步處理結束順序與開始處理順序不一致的情況(我在調試中已經(jīng)發(fā)現(xiàn)該問題)。
針對你提出的這個問題,采用了多線程處理,利用的是BackgroundWorker也就是異步處理控件進行了處理。
代碼已經(jīng)經(jīng)過調試通過。歡迎交流,如有問題,留下QQ或其他聯(lián)系方式。
代碼如下,并附程序截圖。
‘---------------------------------------------------
Imports?System.ComponentModel?'導入異步控件命名空間
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