VB.NET Technology

VB.Net Projects

VB.Net Project 1

What is thread
Previous Home Next
adplus-dvertising

Threads are typically created when you want a program to do two things at once.

Starting Threads

The simplest way to create a thread is to create a new instance of the Thread class. The Thread constructor takes a single argument: a delegate type. The CLR provides the ThreadStart delegate class specifically for this purpose, which points to a method you designate. This allows you to construct a thread and to say to it "when you start, run this method." The ThreadStart delegate declaration is:

Public Delegate Sub ThreadStart()

Example

Imports System.Threading
Public Class CreatingThread
	Private Shared Sub Main(args As String())
		Dim MyThread As New Thread(New ThreadStart(ThreadProc))
		MyThread.Start()
		MyThread.Join()
	End Sub
	Protected Shared Sub ThreadProcess()
		For i As Integer = 0 To 99
			Console.WriteLine(i)
		Next
	End Sub
End Class

Creating a Thread of Execution

Imports System.Threading
Class MyThread
	Public count As Integer
	Private thrdName As String
	Public Sub New(name As String)
		count = 0
		thrdName = name
	End Sub
	Public Sub run()
		Console.WriteLine(thrdName & " starting.")
		Do
			Thread.Sleep(500)
			Console.WriteLine("In " & thrdName & ", count is " & count)
			count += 1
		Loop While count < 10
		Console.WriteLine(thrdName & " terminating.")
	End Sub
End Class
Public Class MultiThread
	Public Shared Sub Main()
		Console.WriteLine("Main thread starting.")
		Dim mt As New MyThread("Child #1")
		Dim newThrd As New Thread(New ThreadStart(AddressOf mt.run))
		newThrd.Start()
		Do
			Console.Write(".")
			Thread.Sleep(100)
		Loop While mt.count <> 10
		Console.WriteLine("Main thread ending.")
	End Sub
End Class

Joining Threads

To join thread1 (t1) onto thread2 (t2), write:
    t2.Join()

Joining the current thread to each thread in the collection in turn :

For Each myThread As Thread In myThreads
	myThread.Join()
Next
Console.WriteLine("All my threads are done.")

Create multiple threads of execution

Imports System.Threading
Class MyThread 	 	     	 	 	 	
    	Public count As Integer	 	 	 	
    	Public thrd As Thread	 	 	 	
    	Public Sub New(name As String)			 			 		 		 		
    		count = 0  	 		 		 		
    		thrd = New Thread(New ThreadStart(AddressOf Me.run))		 		 		 		
    		thrd.Name = name 		 			 		 		 		
    		thrd.Start() 	 	 	
    	End Sub 	 		 	 	 	
    	Private Sub run()  		 		 		 		
    		Console.WriteLine(thrd.Name & " starting.")		 		 		 		
    		Do	 			 			 			 			 			 			 			
    			Thread.Sleep(500) 			 			 			 			
    			Console.WriteLine("In " & thrd.Name & ", count is " & count)		 			 			 			 			
    			count += 1		 				 		 		 		
    		Loop While count < 10		 		 		 		 		 		
    		Console.WriteLine(thrd.Name & " terminating." 	 	 	
    	End Sub
End Class
Public Class MoreThread 	 	 	 	
    	Public Shared Sub Main()				 		 		 		
    		Console.WriteLine("Main thread starting.")			 		 		 		
    		Dim mt1 As New MyThread("Child #1")		 		 		 		
    		Dim mt2 As New MyThread("Child #2")		 		 		 		 		
    		Dim mt3 As New MyThread("Child #3")	 			 		 		 		
    		Do			 	 			 			 			 			 			 			
    			Console.Write(".")	 			 		 			 			 			 			
    			Thread.Sleep(100) 		 		 		 		
    		Loop While mt1.count < 10 OrElse mt2.count < 10 OrElse mt3.count < 10			 		 		
    		Console.WriteLine("Main thread ending.") 	
    	End Sub
End Class

Suspending Threads

To cause your thread to sleep for one second, you can invoke the static method of Thread, Sleep, which suspends the thread in which it is invoked

Thread.Sleep(1000)

Killing Threads

For killing a thread Abort( ) method is used. This causes a ThreadAbortException exception to be thrown, which the thread can catch, and thus provides the thread with an opportunity to clean up any resources it might have allocated.

Example for Suspending, resuming, and stopping a thread

Imports System.Threading
Class MyThread
    	Public thrd As Thread
    	Public Sub New(name As String)
    		thrd = New Thread(New ThreadStart(AddressOf Me.run))	
    		thrd.Name = name	
    	thrd.Start()
    	End Sub
    	Private Sub run()
    		Console.WriteLine(thrd.Name & " starting.")
    		For i As Integer = 1 To 1000
    			Console.Write(i & " ")
    			If (i Mod 10) = 0 Then
    				Console.WriteLine()
    				Thread.Sleep(250)
    			End If
    		Next
    		Console.WriteLine(thrd.Name & " exiting.")
    	End Sub
End Class
Public Class SuspendResumeStop
    	Public Shared Sub Main()
    		Dim mt1 As New MyThread("My Thread")
    		Thread.Sleep(1000)
    		' let child thread start executing 
    		mt1.thrd.Suspend()
    		Console.WriteLine("Suspending thread.")
    		Thread.Sleep(1000)
    		mt1.thrd.[Resume]()
    		Console.WriteLine("Resuming thread.")
    		Thread.Sleep(1000)
    		mt1.thrd.Suspend()
    		Console.WriteLine("Suspending thread.")
    		Thread.Sleep(1000)
    		mt1.thrd.[Resume]()
    		Console.WriteLine("Resuming thread.")
      		Thread.Sleep(1000)
    		Console.WriteLine("Stopping thread.")
    		mt1.thrd.Abort()
       		mt1.thrd.Join()
			' wait for thread to terminate 
    		Console.WriteLine("Main thread terminating.")
    	End Sub
End Class

Synchronization

Synchronization is provided by a lock on the object, which prevents a second thread from barging in on your object until the first thread is finished with it.

Previous Home Next