你需要定義一個公共變量模塊,來存儲線程類里面的變量。這樣在線程結束后變量值都存在模塊變量里面了。當再次開啟線程再把數(shù)據(jù)賦給類里面。
成都網(wǎng)站建設、成都做網(wǎng)站的開發(fā),更需要了解用戶,從用戶角度來建設網(wǎng)站,獲得較好的用戶體驗。創(chuàng)新互聯(lián)公司多年互聯(lián)網(wǎng)經(jīng)驗,見的多,溝通容易、能幫助客戶提出的運營建議。作為成都一家網(wǎng)絡公司,打造的就是網(wǎng)站建設產(chǎn)品直銷的概念。選擇創(chuàng)新互聯(lián)公司,不只是建站,我們把建站作為產(chǎn)品,不斷的更新、完善,讓每位來訪用戶感受到浩方產(chǎn)品的價值服務。
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中如何結束一個線程
一般而言,如果您想終止一個線程,您可以使用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())
當然,在調用Abort方法后,線程并不是立刻終止,要等線程的所有finally快中的代碼完成后才會完全終止. 所以在主線程中可以用Join方法來同步,當線程還未完全終止時,t.Join()將處于等待,直到t線程完全結束后再繼續(xù)執(zhí)行后面的語句。
Abort方法是會導致線程跳出一個異常錯誤的,你需要在代碼中捕獲該異常。下面是一個比較完整的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異常。使終結的線程可以捕獲到異常但是很難控制恢復,僅有的辦法是調用Thread.ResetAbort()來取消剛才的調用,而且只有當這個異常是由于被調用線程引起的異常。因此,A線程可以正確的使用Thread.Abort()方法作用于B線程,但是B線程卻不能調用Thread.ResetAbort()來取消Thread.Abort()操作。
多線程一般是不推薦用的,因為線程之間如果有共享資源的話會引起競爭,需要加鎖處理;而且線程間沒有時序關系,所以你在調試中可能會出現(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