MultiThreading in C#

There are two distinct types of multitasking:
process-based and thread-based. It is important to understand the difference between the two.

  •  Process is what the operating system uses to facilitate the execution of a program by providing the resources required. Each process has a unique process Id associated with it. You can view the process within which a program is being executed using windows task manager.Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently. For example, process-based multitasking allows you to run a word processor at the same time you are using a spreadsheet or browsing the Internet. In process-based multitasking, a program is the smallest unit of code that can be dispatched by the scheduler.
  • Thread is a light weight process. A process has at least one thread which is commonly called as main thread which actually executes the application code. A single process can have multiple threads.In athread-based multitasking environment, all processes have at least one thread, but they can have more. This means that a single program can perform two or more tasks at once. For instance, a text editor can be formatting text at the same time that it is printing, as long as these two actions are being performed by two separate threads.

The differences between process-based and thread-based multitasking can be summarized like this: Process-based multitasking handles the concurrent execution of programs. Thread-based multitasking deals with the concurrent execution of pieces of the same program.

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. So far we wrote the programs where a single thread runs as a single process which is the running instance of the application. However, this way the application can perform one job at a time. To make it execute more than one task at a time, it could be divided into smaller threads.

  • Single threaded Applications Single threaded applications are those in which a thread cannot execute until the earlier thread has completed its execution.  The MS-DOS operating system is an example of a single threaded operating system.  These environments do not have any support for Multithreading and they monopolize the processor and have low system throughput.  Throughput is a measure of the amount of the job done in unit time.
  • Multithreading Applications Multithreading is the ability of the operating system to have at the same point of time multiple threads in memory which switch between the tasks so as to provide a pseudo parallelism, as if all the tasks are running simultaneously.  This illusion of concurrency is ensured by the Operating System by providing a specific time slice to each and every thread and then switching between the threads once their slice is over.  This switching is very fast.  The switching between the threads involves a context switch which in turn involves saving the current thread’s state, flushing the CPU and handling control of the CPU to the next thread in the queue.  Remember that at any point of time the CPU can execute only one thread.  It is to be noted here that Multiprocessing involves multiple processor with each executing one thread at any particular point of time.

Advantages and Disadvantages of Multithreading

  • To maintain a responsive user interface
  • Faster execution
  • To make efficient use of processor time while waiting for I/O operations to complete.
  • To split large, CPU-bound tasks to be processed simultaneously on a machine that has multiple CPUs/cores.
  • Support for Concurrency

Disadvantages of multithreading:

  • On a single-core/processor machine threading can affect performance negatively as there is overhead involved with context-switching.
  • Have to write more lines of code to accomplish the same task.
  • Multithreaded applications are difficult to write, understand, debug and maintain

The following states in the life cycle of a thread are:

  • Unstarted State: When the instance of the thread is created but the Start method is not called.
  • Ready State: When the thread is ready to run and waits for CPU cycle.

Not Runnable State: When thread is in the condition, given below:

  • Sleep method has been called.
  • Wait method has been called.
  • Blocked by I/O operations.

The Dead State: When the thread is in the condition, given below:

  • Completes execution.
  • Aborted.

Below are some most used properties,methods and constructors of threading class.


  • Thread(ThreadStart)  Initializes a new instance of the Thread class.
  • Thread(ParameterizedThreadStart)   Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started.


  • Name  Gets or sets the name of the thread.
  • ThreadState Gets a value containing the states of the current thread.
  • Priority Gets or sets a value indicating the scheduling priority of a thread.
  • ManagedThreadId Gets a unique identifier for the current managed thread.
  • IsAlive Gets a value indicating the execution status of the current thread.
  • CurrentThread Gets the currently running thread.


  • Start() Causes the operating system to change the state of the current instance to ThreadState.Running.
  • Start(Object)  Causes the operating system to change the state of the current instance to ThreadState.Running, and optionally supplies an object containing data to be used by the method the thread executes.
  • Sleep(Int32) Suspends the current thread for the specified number of milliseconds.
  • Join() Blocks the calling thread until the thread represented by this instance terminates, while continuing to perform standard COM andcSendMessage pumping.
  • Finalize() Ensures that resources are freed and other cleanup operations are performed when the garbage collector reclaims the Thread object. (Overrides CriticalFinalizerObject.Finalize().)
  • Abort() Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
  • ToString() Returns a string that represents the current object.(Inherited fromObject.)

Please follow and like us:

Leave a reply

Your email address will not be published.