Monday, September 13, 2010

Basic Threading with dotnet

Hello friends,
Some time I really scared when I listen threading, but when I dive in threading pool its really fun and interesting also.
As per my knowledge what ever I got by learning from different books and web sites I will give you some basic knowledge which you will use and enjoy.
So first thing is what is threading? So here we go. When we talk about multi tasking, multi processing then threading word comes in action. Suppose you have a agile code which will run on a button click but the problem is when user click the button its hang up user screen for a while but you want user screen to be free and allow user to do some other task which he wants. Or similar thing in code also.
In this situation you require threading. It will improve user interaction and performance of your code also.

So start with basic the base class in dot net for threading is System.Threading

I will just write a code how to start a thread so it will much better to understand.

Suppose on button click we want to create a thread which will count number
Private Sub btnStartThread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartThread.Click

Dim myThread As Threading.Thread = New System.Threading.Thread(New Threading.ThreadStart(AddressOf Me.pvt_NumberCounter))
myThread.Start()
MsgBox(“Next statement after thread!!!!”, MsgBoxStyle.Critical, “Rajat”)

End Sub

In the above lines what we are doing we are creating a delegate for a method which pvt_NumberCounter which count the number and assign it to mythread object.
Then to start thread or to activate thread we wrote mythread.start()
Which make thread active. Method pvt_NumberCounter() get in action.
While after mythread.start() statement message box also execute.
Means controls remain in start thread button.

In this way the thread can start.
There are some states of thread which will call thread state by which we can know what is the current position or state of a thread. For understanding thread you must understand thread state. Go through below fig and table.



Action
ThreadState

A thread is created within the common language runtime.
Unstarted

A thread calls Start
Running

The thread starts running.
Running

The thread calls Sleep
WaitSleepJoin

The thread calls Wait on another object.
WaitSleepJoin

The thread calls Join on another thread.
WaitSleepJoin

Another thread calls Interrupt
Running

Another thread calls Suspend
SuspendRequested

The thread responds to a Suspend request.
Suspended

Another thread calls Resume
Running

Another thread calls Abort
AbortRequested

The thread responds to a Abort request.
Stopped

A thread is terminated.
Stopped
















Now your mind has one question we started Thread, we have seen thread state but how to kill a thread explicitly I know you are very destructive mind : D but it was also in my mind so here is simple property which we have to use.
Which is Abort(). Sounds interesting.
You can kill a thread explicitly by thread.abort() statement.
So friends its over for me for this article i will bring some more stuff later on.
till then keep programming, keep visiting.
Thanks
Rajat
Kindle Wireless Reading Device, Wi-Fi, 6" Display, Graphite - Latest Generation