很簡(jiǎn)單,調(diào)用API函數(shù)CreateThread
我們提供的服務(wù)有:成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、雁山ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的雁山網(wǎng)站制作公司
但是用vb做多線程的關(guān)鍵不在于創(chuàng)建,而是在于線程的不穩(wěn)定,其不穩(wěn)定是由于vb6自身的控件(如按鈕、文本框等)不是線程安全的,怎么用你程序就可能怎么掛,所以這些個(gè)玩樣兒在多線程中統(tǒng)統(tǒng)不能用。
那么UI的實(shí)現(xiàn)也得用API實(shí)現(xiàn)。這樣vb的多線程才相對(duì)要穩(wěn)定。但是vb的可視化開發(fā)的優(yōu)勢(shì)又蕩然無存。
過程只需要1個(gè),線程你要多少就需要?jiǎng)?chuàng)建多少,這個(gè)可以用循環(huán)來創(chuàng)建:
Dim Thd(99) As Thread
For i As Integer =0 to 99
Thd(i)=New Thread(AddressOf Start)
Thd(i).Start()
Next
如果你不需要保留這些線程的引用
那個(gè)數(shù)組也可以省略掉
直接在循環(huán)中(New Thread(AddressOf Start)).Start()就行了
---------------------------------------------------------------------------
代碼不是在IDE中打的,可能有細(xì)微小錯(cuò)誤
多線程操控同一個(gè)空間容易出現(xiàn)混亂,可通過SyncLock語句,將對(duì)空間的操作添加至lock語句內(nèi)部,
Class?simpleMessageList
Public?messagesList()?As?String?=?New?String(50)?{}
Public?messagesLast?As?Integer?=?-1
Private?messagesLock?As?New?Object
Public?Sub?addAnotherMessage(ByVal?newMessage?As?String)
SyncLock?messagesLock
messagesLast?+=?1
If?messagesLast??messagesList.Length?Then
messagesList(messagesLast)?=?newMessage
End?If
End?SyncLock
End?Sub
End?Class
參考:
Private Sub setA()
msgbox("")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t1 As Threading.Thread = New Threading.Thread(AddressOf setA)
t1.start
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