Threading

来源:互联网 发布:万网域名cname解析 编辑:程序博客网 时间:2024/05/16 11:48
'Start a Threading 'There're 3 Threadings in this applicationImports System.Threading Public Class Form1     Private trd1 As Thread     Private trd2 As Thread     Private p1 As Integer     Private p2 As Integer     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick         '时时输出p1,p2的值         Label1.Text = Str(p1)         Label2.Text = Str(p2)         Timer1.Stop()         Timer1.Start()   End Sub   

一般而言,如果您想终止一个线程,您可以使用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()操作。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show("this is the main thread") End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Timer1.Start() trd1 = New Thread(AddressOf ThreadTask1) trd1.IsBackground = True trd1.Start() trd2 = New Thread(AddressOf ThreadTask2) trd2.IsBackground = True trd2.Start() End Sub Private Sub ThreadTask1() Dim i, j As Integer Dim f As Boolean i = 1 j = 1 p1 = 1 Do f = True For j = 2 To i - 1 If i Mod j = 0 Then f = False End If Next If f Then p1 = i End If i = i + 1 MessageBox.Show(p1) Loop End Sub Private Sub ThreadTask2() Dim i, j As Integer Dim f As Boolean i = 1 j = 1 p2 = 1 Do f = True For j = 2 To i - 1 If i Mod j = 0 Then f = False End If Next If f Then p2 = i End If i = i + 1 Loop End Sub Private Sub btnTrd1Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTrd1Stop.Click '完全停止一个Threading trd1.Abort() End Sub Private Sub btnTrd2Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTrd2Stop.Click trd2.Abort() End Sub End Class


0 0
原创粉丝点击