vb.net中如何結(jié)束一個(gè)線(xiàn)程
成都創(chuàng)新互聯(lián)主營(yíng)新蔡網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā),新蔡h5微信小程序開(kāi)發(fā)搭建,新蔡網(wǎng)站營(yíng)銷(xiāo)推廣歡迎新蔡等地區(qū)企業(yè)咨詢(xún)
一般而言,如果您想終止一個(gè)線(xiàn)程,您可以使用System.Threading.Thread類(lèi)的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方法后,線(xiàn)程并不是立刻終止,要等線(xiàn)程的所有finally快中的代碼完成后才會(huì)完全終止. 所以在主線(xiàn)程中可以用Join方法來(lái)同步,當(dāng)線(xiàn)程還未完全終止時(shí),t.Join()將處于等待,直到t線(xiàn)程完全結(jié)束后再繼續(xù)執(zhí)行后面的語(yǔ)句。
Abort方法是會(huì)導(dǎo)致線(xiàn)程跳出一個(gè)異常錯(cuò)誤的,你需要在代碼中捕獲該異常。下面是一個(gè)比較完整的VB.NET線(xiàn)程例子:
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()方法用來(lái)永久銷(xiāo)毀一個(gè)線(xiàn)程,而且將拋出ThreadAbortException異常。使終結(jié)的線(xiàn)程可以捕獲到異常但是很難控制恢復(fù),僅有的辦法是調(diào)用Thread.ResetAbort()來(lái)取消剛才的調(diào)用,而且只有當(dāng)這個(gè)異常是由于被調(diào)用線(xiàn)程引起的異常。因此,A線(xiàn)程可以正確的使用Thread.Abort()方法作用于B線(xiàn)程,但是B線(xiàn)程卻不能調(diào)用Thread.ResetAbort()來(lái)取消Thread.Abort()操作。
這個(gè)沒(méi)法做到。原因
1)當(dāng)你將方法排入線(xiàn)程池隊(duì)列后,此方法在有線(xiàn)程池線(xiàn)程變得可用時(shí)執(zhí)行。
2)線(xiàn)程池中某個(gè)線(xiàn)程的可用與不可用,是由.net 后臺(tái)決定,用戶(hù)程序無(wú)法控制的
3)正如你觀察到的:線(xiàn)程啟動(dòng)的時(shí)間不同,有快有慢,這恰恰說(shuō)明線(xiàn)程池起作用了:線(xiàn)程池的調(diào)度試圖讓程序響應(yīng)達(dá)到最佳。
Sub Main()
Dim thr As Thread
For Pi As Integer=0 To 4 //啟用5線(xiàn)程
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)多線(xiàn)程進(jìn)程
Application.DoEvents
Next
End Sub
Public?Class?Form1
Public?Class?SquareClass?'把多線(xiàn)程調(diào)用的函數(shù)封裝到類(lèi)中,通過(guò)類(lèi)事件返回
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?'多線(xiàn)程返回值測(cè)試,當(dāng)線(xiàn)程運(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
名稱(chēng)欄目:vb.net線(xiàn)程池例子 vb6 線(xiàn)程
URL鏈接:http://jinyejixie.com/article24/ddcddce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、App設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)、做網(wǎng)站、軟件開(kāi)發(fā)、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)