blog

Home / DeveloperSection / Blogs / Introduction of Threading in C#

Introduction of Threading in C#

Anonymous User4935 26-Aug-2014

In this blog I’m talking about introduction of Thread and Multithread.

Thread:

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


Multithread:

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:

Introduction of Threading in C#

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.

1.    The Unstarted State: it is the situation when the instance of the thread is created but the Start method has not been called.

2.    The Ready State:it is the situation when the thread is ready to run and waiting CPU cycle.

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

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

using System.Threading;
namespace ThreadDemo
{
    classSampleThread
    {
        publicstaticvoid Display()
        {
            for(int i=0;i<=10;i++)
            {
                Console.WriteLine("Dispaly Thread Start");
            }
        }
        publicstaticvoid Main()
        {
            Thread thread = newThread(newThreadStart(Display));
            Console.WriteLine("Thread Started");
            thread.Start();
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine("Main Thread");
            }
            Console.WriteLine("Thread Compleated");
            Console.ReadKey();
        }
    }
}

Output:

Some Methods of Thread Class:

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

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

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

4.    public void Start():- This method starts a thread.

5.    public static void Sleep(int timemilisecond):- this method pause the thread for a time period.

6.    public static void SpinWait(int iterations):- thread to wait the number of times defined by the iterations parameter.

7.    public static void ResetAbort():- Cancels an abort request for the current thread.

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

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

11.public static AppDomain GetDomain():-Returns a unique application domain identifier.


in my next post i'll explain aboutC# Hashtable


Updated 18-Sep-2014
I am a content writter !

Leave Comment

Comments

Liked By