VB.Net by API/System.Threading/Thread — различия между версиями

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

Текущая версия на 12:50, 26 мая 2010

Thread.Abort()

  
Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.IO

public class MainClass
   Shared Sub Main()
        Dim Thrd As Thread
        Dim TStart As New ThreadStart(AddressOf BusyThread)
        Thrd = New Thread(TStart)
        Thrd.Priority = ThreadPriority.Highest
        Thrd.Start()
        Console.WriteLine(Thrd.ThreadState.ToString("G"))
        Console.WriteLine("Thrd.IsAlive " & Thrd.IsAlive)
        If Thrd.IsAlive Then
            Thrd.Abort()
            Thrd.Join()
        End If
   End Sub
   Shared Sub BusyThread()
        While True
            "Console.Write("thread ")
        End While
    End Sub
End Class


Thread.CurrentPrincipal

  
 
Imports System.Threading
Module Module1
    Sub Main()
        Dim objThread As Thread
        objThread = Thread.CurrentThread
        With objThread
            Console.WriteLine("Apartment State: {0}", .ApartmentState)
            Console.WriteLine("Current Context: {0}", .CurrentContext)
            Console.WriteLine("Current Culture: {0}", .CurrentCulture)
            Console.WriteLine("Current Principal: {0}", .CurrentPrincipal)
            Console.WriteLine("Current Thread: {0}", .CurrentThread)
            Console.WriteLine("Current UI Culture: {0}", .CurrentUICulture)
            Console.WriteLine("Is Alive: {0}", .IsAlive)
            Console.WriteLine("Is Background: {0}", .IsBackground)
            Console.WriteLine("Is Thread Pool Thread: {0}", .IsThreadPoolThread)
            Console.WriteLine("Name: {0}", .Name)
            Console.WriteLine("Priority: {0}", .Priority)
            Console.WriteLine("Thread State: {0}", .ThreadState)
        End With
    End Sub
End Module


Thread.CurrentThread

  
Imports System
Imports System.Data
Imports System.Globalization
Imports System.Threading
public class Test
   public Shared Sub Main
        Thread.CurrentThread.CurrentCulture = New CultureInfo("de-DE")
        Thread.CurrentThread.CurrentUICulture = New CultureInfo("de-DE")
   End Sub
End class


Thread.IsAlive

  

Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.IO

public class MainClass
   Shared Sub Main()
        Dim Thrd As Thread
        Dim TStart As New ThreadStart(AddressOf BusyThread)
        Thrd = New Thread(TStart)
        Thrd.Priority = ThreadPriority.Highest
        Thrd.Start()
        Console.WriteLine(Thrd.ThreadState.ToString("G"))
        Console.WriteLine("Thrd.IsAlive " & Thrd.IsAlive)
        If Thrd.IsAlive Then
            Thrd.Abort()
            Thrd.Join()
        End If
   End Sub
   Shared Sub BusyThread()
        While True
            "Console.Write("thread ")
        End While
    End Sub
End Class


Thread.Join()

  
Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.IO

public class MainClass
   Shared Sub Main()
        Dim Thrd As Thread
        Dim TStart As New ThreadStart(AddressOf BusyThread)
        Thrd = New Thread(TStart)
        Thrd.Priority = ThreadPriority.Highest
        Thrd.Start()
        Console.WriteLine(Thrd.ThreadState.ToString("G"))
        Console.WriteLine("Thrd.IsAlive " & Thrd.IsAlive)
        If Thrd.IsAlive Then
            Thrd.Abort()
            Thrd.Join()
        End If
   End Sub
   Shared Sub BusyThread()
        While True
            "Console.Write("thread ")
        End While
    End Sub
End Class


Thread.Resume

  
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


Thread.Sleep

  
 
Imports System.Threading
Module Module1
    Sub TaskA()
        Dim I As Integer
        Thread.Sleep(1000)
        For I = 0 To 250
            System.Console.Write("A")
        Next
    End Sub
    Sub TaskB()
        Dim I As Integer
        Thread.Sleep(500)
        For I = 0 To 250
            System.Console.Write("B")
        Next
    End Sub
    Sub TaskC()
        Dim I As Integer
        Thread.Sleep(100)
        For I = 0 To 250
            System.Console.Write("C")
        Next
    End Sub
    Sub Main()
        Dim A As Thread  = New Thread(AddressOf TaskA)
        Dim B As Thread  = New Thread(AddressOf TaskB)
        Dim C As Thread  = New Thread(AddressOf TaskC)
        A.Start()
        B.Start()
        C.Start()
    End Sub
End Module


Thread.Suspend

  

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