Thursday 22 December 2011

Threading in DotNet

What is Multi-tasking ?


It’s a feature of modern operating systems with which we can run multiple programs at same time example Word, Excel etc.

What is Multi-threading ?


Multi-threading forms subset of Multi-tasking. Instead of having to switch between programs this feature switches between different parts of the same program. Example you are writing in word and at the same time word is doing a spell check in background.

What is a Thread ?


A thread is the basic unit to which the operating system allocates processor time.

Did VB support multi-threading ?


While VB supports multiple single-threaded apartments, it does not support a freethreading model, which allows multiple threads to run against the same set of data.

Can we have multiple threads in one App domain ?


One or more threads run in an AppDomain. An AppDomain is a runtime representation of a logical process within a physical process. Each AppDomain is started with a single thread, but can create additional threads from any of its threads.

Note :- All threading classes are defined in System.Threading namespace.



Which namespace has threading ?


Systems.Threading has all the classes related to implement threading. Any .NET application who wants to implement threading has to import this namespace.

Note :- .NET program always has at least two threads running one is the main program and second is the garbage collector.



Can you explain in brief how can we implement threading ?


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim pthread1 As New Thread(AddressOf Thread1)
Dim pthread2 As New Thread(AddressOf Thread2)
pthread1.Start()
pthread2.Start()


End Sub
Public Sub Thread1()


Dim pintcount As Integer
Dim pstr As String
pstr = “This is first thread”
Do Until pintcount > 5


lstThreadDisplay.Items.Add(pstr)
pintcount = pintcount + 1


Loop


End Sub
Public Sub Thread2()


Dim pintcount As Integer
Dim pstr As String
pstr = “This is second thread”
Do Until pintcount > 5


lstThreadDisplay.Items.Add(pstr)
pintcount = pintcount + 1


Loop


End Sub


Above is a sample code which shows simple sample code for threading. Above sample code can be found in “Threading” folder in CD provided. Above sample has two methods “Thread1()” and “Thread2()” which are started in multi-threaded mode in Form load event of the sample.

Note :- If you run the sample you will see that sometimes the first thread runs first and then the second thread.This happens because of thread priorities . The first thread is run with highest priority.

How can we change priority and what the levels of priority are provided by .NET ?


Thread Priority can be changed by using Threadname.Priority = ThreadPriority.Highest. In the sample provided look out for code where the second thread is ran with a high priority.

Following are different levels of Priority provided by .NET :-

  • ThreadPriority.Highest

  • ThreadPriority.AboveNormal

  • ThreadPriority.Normal

  • ThreadPriority.BelowNormal

  • ThreadPriority.Lowest

No comments:

Post a Comment