有的话给参考下,我也想这么干 刚学vb 不太懂你意思就随便说说吧声明窗口 Dim aa As New Form 打开已声明的窗口 aa.show 有模式地打开窗口 aa.
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()操作.
把一个线程对一个sub的方法放到For循环中就是了啊,循环的次数你自己设定
不用多线程?
即使用多线程,也不会是"同时"执行,题主只要略懂一些计算机编译原理就能明白了.
不用多线程更不可能让两个过程同步执行了.
不过可以提供一个折衷的法子,示例如下:
Sub?a()
'过程一?
End?Sub?
Sub?b()
'过程二
End?Sub
Sub?Timer1_Tick()?'简写了,实际在VB.NET里不是这样的
Timer1.Enabled?=?False?
a()
End?Sub??
Sub?Button1_Click()?'同样简写
Timer1.Enabled?=?True
b()
利用了一个 Timer 控件,实现了这种伪同步(其实还是异步),a()、b() 两个过程相互独立切能并发运行.
注意:上述代码需要先把 Timer1 的 Enabled 属性设置为 False,Interval 属性要尽可能小.
楼上回答不对,这个问题很常见,就是在线程中使用到了控件,在这种状况下,不能直接操作控件属性,而只能通过委托的方法去实现
你真心无聊.
主线程只是通知系统,请启动一个线程运行某某函数.
以上就是土嘎嘎小编为大家整理的vb.net同步线程相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!