新聞中心
vb.net中如何結(jié)束一個線程
vb.net中如何結(jié)束一個線程
當(dāng)陽網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
一般而言,如果您想終止一個線程,您可以使用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()操作。
vb.net 怎樣操作帶參數(shù)的多線程
public class threadclass
{
public int a;
public void threadmethod()
{
//use a;
}
}
...
threadclass tc = new ....
tc.a = 10;
Thread t = new ThreadStart(tc.threadmethod);
t.Start
vb.net里如何設(shè)置多線程?
首先,你把你那些要運(yùn)行很久的過程。盡量放在一個過程中。
因?yàn)榫€程只能是過程,不能使函數(shù),沒有返回值的。
然后,在某個事件下這樣寫:
**這里我假設(shè)你的那個很就的過程叫做 sub aaa()
調(diào)用如下:
Dim mythread As New System.Threading.Thread(AddressOf aaa)
mythread.Start()
***********
這樣就是定義一個線程,名字叫 mythread,這個線程會運(yùn)行aaa這個過程。
start 就是讓線程運(yùn)行。
VB.NET 多線程簡化步驟問題
過程只需要1個,線程你要多少就需要創(chuàng)建多少,這個可以用循環(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
如果你不需要保留這些線程的引用
那個數(shù)組也可以省略掉
直接在循環(huán)中(New Thread(AddressOf Start)).Start()就行了
---------------------------------------------------------------------------
代碼不是在IDE中打的,可能有細(xì)微小錯誤
VB.net 如何設(shè)計(jì)多線程運(yù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() //啟動多線程進(jìn)程
Application.DoEvents
Next
End Sub
本文標(biāo)題:vb.net教程線程 VB多線程實(shí)例
本文來源:http://fisionsoft.com.cn/article/dodsiig.html