*** Create a new Thread ***
Imports System.Threading 'Imports the System.Threading namespace.
Module create_Thread
Sub Main(ByVal args As String())
' create a new thread
Dim th As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))
' start the newly created thread
th.Start()
Console.WriteLine(" It is a Main Thread")
End Sub
Private Sub PrintInfo()
For j As Integer = 1 To 5
Console.WriteLine(" value of j is {0}", j)
Next j
Console.WriteLine(" It is a child thread")
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module
*** VB.NET Thread Life Cycle ***
Imports System.Threading
Module Thread_cycle
Sub Main(ByVal args As String())
Dim s As Stopwatch = New Stopwatch()
s.Start()
Dim t As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))
t.Start()
' Halt another thread execution until the thread execution completed
t.Join()
s.[Stop]()
Dim t1 As TimeSpan = s.Elapsed
Dim ft As String = String.Format("{0}: {1} : {2}", t1.Hours, t1.Minutes, t1.Seconds)
Console.WriteLine(" Total Elapsed Time : {0}", ft)
Console.WriteLine("Completion of Thread Execution ")
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Sub
Private Sub PrintInfo()
For j As Integer = 1 To 6
Console.WriteLine(" Halt Thread for {0} Second", 5)
' It pause thread for 5 Seconds
Thread.Sleep(5000)
Console.WriteLine(" Value of i {0}", j)
Next
End Sub
End Module
*** Multithreading ***
Imports System.Threading
Module Multi_thread
Sub Main(ByVal arg As String())
Dim th As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))
Dim th2 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo2))
th.Start()
th2.Start()
Console.ReadKey()
End Sub
Private Sub PrintInfo()
For j As Integer = 1 To 5
Console.WriteLine(" value of j is {0}", j)
Thread.Sleep(1000)
Next
Console.WriteLine(" Completion of First Thread")
End Sub
Private Sub PrintInfo2()
For k As Integer = 1 To 5
Console.WriteLine(" value of k is {0}", k)
Next
Console.WriteLine(" Completion of First Thread")
End Sub
End Module
沒有留言:
張貼留言