Tuesday, July 19, 2011

vb.net multi threading

Multithreading

Multithreading gives programs the ability to do several things at a time. Each stream of execution is called a thread. Multithreading is used to divide lengthy tasks into different segments that would otherwise abort programs. Threads are mainly used to utilize the processor to a maximum extent by avoiding it's idle time. Threading lets a program seem as if it is executing several tasks at once. What actually happens is, the time gets divided by the computer into parts and when a new thread starts, that thread gets a portion of the divided time. Threads in VB .NET are based on the namespace System.Threading.
Creating Threads
To create threads lets work with an example. The following example is an extract from Steven Holzner's reference, Programming with Visual Basic.NET- Black Book. Open anew windows application and name it as Thread and add a class named count1 using the Projects->Add Class item. This class will count from 1 to a specified value in a data member named CountTo when you call the Count method. After the count has reached the value in CountTo, a FinishedCounting event will  occur. The code for the Count class looks like this:
Public Class Count1
Public CountTo as Integer
Public event FinishedCounting(By Val NumberOfMatches as Integer)
Sub Count()
Dim ind,tot as Integer
tot=0
For ind=1 to CountTo
tot+=1
Next ind
RaiseEvent FinishedCounting(tot)
'makes the FinishedCounting event to occur
End Sub
End Class

Let's use this class with a new thread.  Get back to the main form and create an object of this class, counter1, and a new thread, Thread1. The code looks like this:
Public Class Form1 Inherits System.Windows.Forms.Form
Dim counter1 as new Count1()
Dim Thread1 as New System.Threading.Thread(Address of counter.Count)

Drag a Button and two TextBoxes (TextBox1, TextBox2) onto the form. Enter a number in TextBox1. The reason for entering a number in textbox is to allow the code to read the value specified in TextBox1 and display that value in TextBox2, with threading. The code for that looks like this:
Public Class Form1 Inherits System.Windows.Forms.Form
Dim counter1 as new Count1()
Dim Thread1 as New System.Threading.Thread(Address of counter.Count)
Private Sub Button1_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button1.Click
TextBox2.Text=" "
counter1.CountTo=TextBox1.Text
AddHandler counter1.FinishedCounting,AddressOfFinishedCountingEventHandler
'adding handler to handle FinishedCounting Event
Thread1.Start()
'starting the thread
End Sub
Sub FinishedCountingEventHandler(ByVal Count as Integer)
'FinishedCountingEventHandler
TextBox2.Text=Count
End Sub

The result of the above code displays the value entered in TextBox1, in TextBox2 with the difference being the Thread counting the value from 1 to the value entered in TextBox1.
Multithreading

Suspending a Thread
Threads can be suspended. Suspending a thread stops it temporarily. Working with the example in the previous section, add a new button Button2 to the main form. When this button is clicked the thread is suspended. The code for that looks like this:
Private Sub Button2_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button2.Click
Thread1.Suspend()
End Sub

Resuming a Thread
Threads can be resumed after they are suspended. With the example above, add a new button Button3 to the main form. When this button is clicked the thread is resumed from suspension. The code for that looks like this:
Private Sub Button3_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button3.Click
Thread1.Resume()
End Sub

Making a Thread Sleep
Threads can be made to sleep which means that they can be suspended over a specific period of time. Sleeping a thread is achieved by passing the time (in milliseconds,1/1000 of a second) to the thread's sleep method. With the example above, add a new button Button4 to the main form. When this button is clicked the thread is stopped. The code for that looks like this:

Private Sub Button4_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button4.Click
Thread1.Sleep(100/1000)
End Sub

Stopping a Thread
Threads can be stopped with it's abort method. With the example above, add a new button Button5 to the main form. When this button is clicked the thread is stopped. The code for that looks like this:
Private Sub Button5_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button5.Click
Thread1.Abort()
End Sub

Thread Priorities
Threads can also be assigned priority for execution. Thread priority can be set by the thread's Priority property and assigning a value from predefined Thread Priority enumeration.
Values for Thread Priority:
Above Normal -> Gives thread higher priority
Below Normal ->Gives thread lower priority
Normal -> Gives thread normal priority
Lowest -> Gives thread lowest priority
Highest -> Gives thread highest priority
Working with the above example, add a new button Button6 to the main form. When this button is clicked the thread is assigned Highest priority .The code for that looks like this:
Private Sub Button6_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button6.Click
Thread1.Priority=System.Threading.ThreadPriority.Highest
'setting Highest priority for the thread
End Sub

For more information on Threads please refer to MSDN.

No comments:

Post a Comment