135-1821-9792

vb点虐 判断线程结束 c#判断线程是否存活

vb点虐 中如何结束一个线程

vb点虐 中如何结束一个线程

创新互联公司主要从事网站设计、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务伊犁,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

一般而言,如果您想终止一个线程,您可以使用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线程完全结束后再继续执行后面的语句。

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()操作。

vb点虐 如何判断线程执行完毕?

我给你一个简单的思路:

1,定义5个公共布尔变量,用来记录5个线程的执行情况

2,线程开始变量赋值假,执行完毕或者异常退出赋值真

3,一直用一个定时器扫描5个公共布尔变量----------随时知道状态

vb点虐 判断过程是否执行完

最简单的方法,在过程结束前加一个判断标志,判断这个标志就可以知道是不是结束了


网页题目:vb点虐 判断线程结束 c#判断线程是否存活
网页网址:http://kswsj.com/article/ddgeejj.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号