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))
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.




A thread is created within the common language runtime.


A thread calls Start


The thread starts running.


The thread calls Sleep


The thread calls Wait on another object.


The thread calls Join on another thread.


Another thread calls Interrupt


Another thread calls Suspend


The thread responds to a Suspend request.


Another thread calls Resume


Another thread calls Abort


The thread responds to a Abort request.


A thread is terminated.









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.

How to Start and kill a process (exe) From dot net

Hello friends,
First of all sorry for such a late post bit busy in some other stuff but come back with bang.
Today I will show you some simple but very interesting topic. So first topic is how to call a process from your code. And how to kill a particular process from your code.
Its code is very simple just needs to understand once then you are perfect in this.
The first thing you need to know there is base class for process which is System.Diagnostics
I am showing you code right here so please with me for few moments.
I am opening a Microsoft word application here.

Imports System
Imports System.Diagnostics
Imports System.Diagnostics.Process

Private Sub btnStartProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartProcess.Click
Dim oProcess As Process
Dim startInfo As New ProcessStartInfo
startInfo.FileName = “winword.exe”
oProcess = Process.Start(startInfo)
Catch ex As Exception
Throw ex
End Try
End Sub

In this way you can open a new Microsoft word application in similar manner you can open different other process. And also pass. Command line arguments to them.

Now if you want to close this application then its also easy. Just see below code and you will be master in that.

Private Sub btncloseProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncloseProcess.Click
Dim oProcess As Process
Catch ex As Exception
Throw ex
End Try
End Sub

If you know the id of process then it could be more specific. To kill particular instance.
In this way you can create and kill a process.
In next session I will come up with some more interesting topics till than do your best enjoy programming, enjoy dot net.
Thanks & regards