Introduction of Threading in C#
In this blog I’m talking about introduction of
Thread and Multithread.
application runs with at least one thread. So what is a thread? A
thread is nothing more than a process. On the computer, a thread is a process
moving through time. The process performs sets of sequential steps, each
step executing a line of code. Because the steps are sequential, each
step takes a given amount of time. The time it takes to complete a series
of steps is the sum of the time it takes to perform each programming step. C#
supports parallel execution of code through multithreading. A thread is an
independent execution path, able to run simultaneously with other threads. Threads
are lightweight processes. One common example of use of thread is
implementation of concurrent programming by modern operating systems. Use of
threads saves wastage of CPU cycle and increase efficiency of an application.
For a long time most programming application were
single threaded except system programs that means in that application only one
thread entire whole application. multithreading is used to perform multiple
tasks at the same time. Tasks with the potential of holding up other tasks can
execute on separate threads, a process known as multithreading. Or, it's
basically trying to do more than one thing at a time within a process. You could never do Execution
AA until completing Execution BB. Another way to think of multiple
threading is to consider the human body. Each one of the body's parts
(heart, lungs, liver, brain) are all involved in processes. Each process
is running simultaneously. Imagine if each organ run as a step in a
process, first the heart, then the brain, then the liver, then the lungs.
We would probably drop dead. So the human body is like one big
multithreaded application. All organs are processes running
simultaneously, and all of these processes depend upon one another.
Life Cycle of Thread:
The life cycle of a thread starts when an object of the
System.Threading.Thread class is created and ends when the thread is terminated
or completes execution.
Following are the various states in the life cycle of a thread.
The Unstarted State: it is the situation when the instance of the thread is
created but the Start method has not been called.
The Ready State: it is the situation when the thread is ready to run and waiting
The Not Runnable State: a thread is not runnable, when:
a. Sleep method has been called
b. Wait method has been called
c. Blocked by I/O operations
The Dead State: it is the situation when the thread has completed
execution or has been aborted.
Create Thread With Thread Class:
In this example I’m creating two thread which are
execute currently without effecting each other. First thread is Main Thread
public static void Display()
public static void Main()
Thread thread = new Thread(new ThreadStart(Display));
for (int i = 0; i <= 10; i++)
Some Methods of Thread Class:
Public void Abort ():-Raises
a ThreadAbortException in the thread on
which it is invoked, to begin the process of terminating the thread while also
providing exception information about the thread termination. Calling this
method usually terminates the thread.
public static LocalDataStoreSlot AllocateDataSlot():- Allocates
unnamed data slot on all the threads for better performance use fields that are
marked with the ThreadStaticAttribute attribute instead.
public static void BeginThreadAffinity ():- Notifies a
host that managed code
is about to execute instructions that
depend on the identity of the current physical operating system thread.
public void Start():- This method starts a thread.
public static void Sleep(int timemilisecond):- this method
pause the thread for a time period.
public static void SpinWait(int
to wait the number of times defined by the iterations parameter.
public static void ResetAbort():- Cancels an abort request for the current
public void Join():-Blocks the calling thread until a
thread terminates, while continuing to perform standard COM and SendMessage
pumping. This method has different overloaded forms.
public void interrupt():- Use this method thread is in the
WaitSleepJoin thread state.
10. public static AppDomain
GetDomain():-Returns the current domain in which
the current thread is running.
static AppDomain GetDomain():-Returns a unique
application domain identifier.
in my next post i'll
explain about C# Hashtable