VB.Net Tutorial/Thread/Thread Operation

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

Thread complete event

Imports System.Threading
Public Class Tester
    Shared WithEvents oSquare As SquareClass = New SquareClass()
    Public Shared Sub Main
        
        Dim t As Thread
        t = New Thread(AddressOf oSquare.CalcSquare)
        oSquare.Value = 3
        t.Start()
    
    End Sub
    Shared Sub SquareEventHandler(ByVal Square As Double) Handles oSquare.ThreadComplete
        Console.WriteLine(Square)
    End Sub
End Class
Public Class SquareClass
    Public Value As Double
    Public Square As Double
    Public Event ThreadComplete(ByVal Square As Double)
    Public Sub CalcSquare()
        Square = Value * Value
        RaiseEvent ThreadComplete(Square)
    End Sub
End Class
9

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
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"