VB.Net/Thread/Suspend Resume

Материал из VB Эксперт
Перейти к: навигация, поиск

Suspend and resume Thread

Imports System.Threading
Module Module1
    Public A As Thread = New Thread(AddressOf MethodA)
    Public B As Thread = New Thread(AddressOf MethodB)
    Public C As Thread = New Thread(AddressOf MethodC)
    Sub MethodA()
        Dim I As Integer
        Try
            A.Suspend()
            For I = 0 To 250
                Console.Write("A")
            Next
        Catch E As ThreadStateException
            Console.WriteLine("Thread A Expection" & E.ToString())
        End Try
    End Sub
    Sub MethodB()
        Dim I As Integer
        Try
            For I = 0 To 250
                Console.Write("B")
            Next
        Catch E As ThreadAbortException
            Console.Write("Thread B has been aborted")
        End Try
    End Sub
    Sub MethodC()
        Dim I As Integer
        Try
            If (B.IsAlive) Then
                B.Abort()
            End If
            For I = 0 To 250
                If (A.ThreadState = ThreadState.Suspended) Then
                    A.Resume()
                End If
                Console.Write("C")
            Next
        Catch E As Threading.ThreadStateException
            Console.WriteLine("Thread C Expection" & E.ToString())
        End Try
    End Sub
    Sub Main()
        A.Start()
        B.Start()
        C.Start()
    End Sub
End Module