VB.NET•Create Threads Simply

Listing 1. You can run methods on newly created threads easily by creating new thread types. This example shows the simplest way to create threads. It also shows how to execute methods using the AddressOf operator in Visual Basic .NET.

Imports System
Imports System.Threading

Module Module1
   ' private variables of type 
   ' System.Threading.Thread
   Private t1 As Thread
   Private t2 As Thread

   Sub Main()
      ' Create new instances of Thread 
      ' variables and pass the method 
      ' that will execute on the thread. The 
      ' Threader1 and Threader2 methods are 
      ' not shown, but they would be executed 
      ' on the corresponding threads that you 
      ' created named t1 and t2

      t1 = New Thread(AddressOf Threader1)
      t2 = New Thread(AddressOf Threader2)

      ' Give threads a name. 
      ' This is useful for debugging
      t1.Name = "Threader1"
      t2.Name = "Threader2"

      ' start both threads
      t1.Start()
      t2.Start()

      ' wait for enter key to be hit
      Console.ReadLine()
    End Sub
End Module