多線程一般是不推薦用的,因為線程之間如果有共享資源的話會引起競爭,需要加鎖處理;而且線程間沒有時序關(guān)系,所以你在調(diào)試中可能會出現(xiàn)異步處理結(jié)束順序與開始處理順序不一致的情況(我在調(diào)試中已經(jīng)發(fā)現(xiàn)該問題)。
成都網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、重慶網(wǎng)站建設(shè)、微信開發(fā)、微信平臺小程序開發(fā)、集團企業(yè)網(wǎng)站設(shè)計等服務(wù)項目。核心團隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:垃圾桶等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致好評!
針對你提出的這個問題,采用了多線程處理,利用的是BackgroundWorker也就是異步處理控件進行了處理。
代碼已經(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
vb.net中如何結(jié)束一個線程
一般而言,如果您想終止一個線程,您可以使用System.Threading.Thread類的Abort方法. 例如:
Dim worker As ThreadStart = New ThreadStart(AddressOf workerthreadmethod)
Dim t As Thread = New Thread(worker)
t.Start()
MessageBox.Show("Wait for a while for the thread to start.")
MessageBox.Show(t.ThreadState.ToString())
t.Abort()
MessageBox.Show(t.ThreadState.ToString())
t.Join()
MessageBox.Show(t.ThreadState.ToString())
當(dāng)然,在調(diào)用Abort方法后,線程并不是立刻終止,要等線程的所有finally快中的代碼完成后才會完全終止. 所以在主線程中可以用Join方法來同步,當(dāng)線程還未完全終止時,t.Join()將處于等待,直到t線程完全結(jié)束后再繼續(xù)執(zhí)行后面的語句。
Abort方法是會導(dǎo)致線程跳出一個異常錯誤的,你需要在代碼中捕獲該異常。下面是一個比較完整的VB.NET線程例子:
Imports System
Imports System.Threading
Public Class MyTestApp
Public Shared Sub Main()
Dim t As New Thread(New ThreadStart(AddressOf MyThreadMethod))
'Start the thread
t.Start()
MsgBox("Are you ready to kill the thread?")
'Kill the child thread and this will cause the thread raise an exception
t.Abort()
' Wait for the thread to exit
t.Join()
MsgBox("The secondary thread has terminated.")
End Sub
Shared Sub MyThreadMethod()
Dim i As Integer
Try
Do While True
Thread.CurrentThread.Sleep(1000)
Console.WriteLine("This is the secondary thread running.")
Loop
Catch e As ThreadAbortException
MsgBox("This thread is going to be terminated by the Abort method in the Main function")
End Try
End Sub
End Class
Thread.Abort()方法用來永久銷毀一個線程,而且將拋出ThreadAbortException異常。使終結(jié)的線程可以捕獲到異常但是很難控制恢復(fù),僅有的辦法是調(diào)用Thread.ResetAbort()來取消剛才的調(diào)用,而且只有當(dāng)這個異常是由于被調(diào)用線程引起的異常。因此,A線程可以正確的使用Thread.Abort()方法作用于B線程,但是B線程卻不能調(diào)用Thread.ResetAbort()來取消Thread.Abort()操作。
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
分享題目:vb.net教程線程 vb怎么實現(xiàn)多線程
鏈接地址:http://jinyejixie.com/article46/hpcpeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、定制開發(fā)、網(wǎng)站營銷、用戶體驗、軟件開發(fā)、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)