用
t.Suspend() 这个是挂起线程.是一个异步方法.
t.Sleep 我看了是一个同步方法.要等这个方法运行完了才继续下一步.
这和from.show和 from.showdiaglog区别差不多!
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()
t.Join()
当然,在调用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
MsgBox("Are you ready to kill the thread?")
'Kill the child thread and this will cause the thread raise an exception
' Wait for the thread to exit
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 Class
Thread.Abort()方法用来永久销毁一个线程,而且将抛出ThreadAbortException异常.使终结的线程可以捕获到异常但是很难控制恢复,仅有的办法是调用Thread.ResetAbort()来取消刚才的调用,而且只有当这个异常是由于被调用线程引起的异常.所以呢,A线程可以正确的使用Thread.Abort()方法作用于B线程,但是B线程却不能调用Thread.ResetAbort()来取消Thread.Abort()操作.
使用api
.net 属于托管资源,可以用GC回收. GC.COLLECT() 好像是这个,如果存在内存问题,可以仔细学下GC回收机制
直接:
Label1.Visible = True
setB()
MsgBox("d")
这不就是执行完setB(),再执行MsgBox("d"),多线程是多余的.
多线程的作用大多数也就用来同时执行多任务而不用等待的.
用time控件啊,获取threadstate后,按指定时间间隔规定向界面刷新,线程必须是全局变量
用一个有退出条件的无限循环的sub也行.
线程开始就触发该过程,用无限循环来刷新状态,线程完成即退出无限循环.局部变量的线程也可以用这种方式.
不过用法要正确,否则会出问题.
以上就是土嘎嘎小编为大家整理的vb.net线程销毁相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!