VB.Net/Thread/Thread Basics

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

Define method and Run inside a Thread

<source lang="vbnet"> Imports System.Threading Public Class MainClass

  Public Shared Sub Main()
       Dim A As Thread = New Thread(AddressOf MethodInThread)
       Dim B As Thread = New Thread(AddressOf MethodInThread)
       Dim C As Thread = New Thread(AddressOf MethodInThread)
       A.Name = "A"
       A.Start()
       B.Name = "B"
       B.Start()
       C.Name = "C"
       C.Start()
  End Sub
  Shared Sub MethodInThread()
       Dim O As Thread
       Dim hash As Integer
       O = Thread.CurrentThread
       hash = O.GetHashCode()
       Console.Write("Thread name: {0}", Thread.CurrentThread.Name)
       Console.WriteLine(" Thread hash: {0}", hash.ToString())
   End Sub

End Class


      </source>


Put time-consuming Task in Thread

<source lang="vbnet"> Imports System Imports System.Threading Public Class MainClass

   Public Shared Sub Main()
       Dim aThreadStart As New Threading.ThreadStart(AddressOf Adding)
       Dim aThread As New Thread(aThreadStart)
       aThread.Name = "increasing thread"
       
       Dim bThreadStart As New Threading.ThreadStart(AddressOf Substracting)
       Dim bThread As New Thread(AddressOf Substracting)
       bThread.Name = "decreasing thread"
       
       aThread.Start()
       bThread.Start()
   End Sub
 Shared Public Sub Adding()
   Dim count As Integer
   Do While True
     count += 1
     Console.WriteLine("Am in thread named " & Thread.CurrentThread.Name & " counter  =" & count)
     If count Mod 100 = 0 Then
       Try
         Thread.Sleep(1000)
       Catch e As ThreadInterruptedException
       End Try
     End If
   Loop
 End Sub
 Shared Public Sub Substracting()
   Dim count As Integer
   Do
     count -= 1
     Console.WriteLine("Am in thread named " & Thread.CurrentThread.Name & " counter  =" & count)
     If count Mod 200 = 0 Then
       Try
         Thread.Sleep(1000)
       Catch e As ThreadInterruptedException
         "thread interrupted from sleeping   
       End Try
     End If
   Loop
 End Sub

End Class


      </source>


Shows multiple threads that print at different intervals

<source lang="vbnet"> Imports System Imports System.Threading Public Class MainClass

  Shared Sub Main()
     Dim printer1 As New Messager()
     Dim printer2 As New Messager()
     Dim printer3 As New Messager()
     Dim thread1 As New Thread(AddressOf printer1.Print)
     Dim thread2 As New Thread(AddressOf printer2.Print)
     Dim thread3 As New Thread(AddressOf printer3.Print)
     thread1.Name = "thread1"
     thread2.Name = "thread2"
     thread3.Name = "thread3"
     Console.WriteLine("Starting threads")
     thread1.Start()
     thread2.Start()
     thread3.Start()
     Console.WriteLine("Threads started" & vbCrLf)
  End Sub " Main

End Class

Public Class Messager

  Private sleepTime As Integer
  Private Shared randomObject As New Random()
  Public Sub New()
     sleepTime = randomObject.Next(5001)
  End Sub " New
  Public Sub Print()
     Dim current As Thread = Thread.CurrentThread
     Console.WriteLine(current.Name & " going to sleep for " & _
        sleepTime)
     Thread.Sleep(sleepTime)
     Console.WriteLine(current.Name & " done sleeping")
  End Sub " Print

End Class

      </source>


Start a Thread

<source lang="vbnet"> 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()
  End Sub
  Shared Sub BusyThread()
       While True
           Console.WriteLine("in thread")
       End While
   End Sub

End Class


      </source>