vb.net線程池例子 vb6 線程

vb.net中如何結束一個線程

vb.net中如何結束一個線程

成都創(chuàng)新互聯(lián)主營新蔡網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,重慶APP開發(fā),新蔡h5微信小程序開發(fā)搭建,新蔡網(wǎng)站營銷推廣歡迎新蔡等地區(qū)企業(yè)咨詢

一般而言,如果您想終止一個線程,您可以使用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())

當然,在調(diào)用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異常。使終結的線程可以捕獲到異常但是很難控制恢復,僅有的辦法是調(diào)用Thread.ResetAbort()來取消剛才的調(diào)用,而且只有當這個異常是由于被調(diào)用線程引起的異常。因此,A線程可以正確的使用Thread.Abort()方法作用于B線程,但是B線程卻不能調(diào)用Thread.ResetAbort()來取消Thread.Abort()操作。

多線程 線程池 VB.NET

這個沒法做到。原因

1)當你將方法排入線程池隊列后,此方法在有線程池線程變得可用時執(zhí)行。

2)線程池中某個線程的可用與不可用,是由.net 后臺決定,用戶程序無法控制的

3)正如你觀察到的:線程啟動的時間不同,有快有慢,這恰恰說明線程池起作用了:線程池的調(diào)度試圖讓程序響應達到最佳。

VB.net 如何設計多線程運行

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多線程如何返回參數(shù),舉個例子,謝謝

Public?Class?Form1

Public?Class?SquareClass?'把多線程調(diào)用的函數(shù)封裝到類中,通過類事件返回

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?'多線程返回值測試,當線程運行完成激發(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?'響應事件函數(shù)

MsgBox("The?square?is?"??Square)

End?Sub

End?Class

名稱欄目:vb.net線程池例子 vb6 線程
URL鏈接:http://muchs.cn/article24/ddcddce.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、App設計品牌網(wǎng)站設計、做網(wǎng)站、軟件開發(fā)、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)