Home > Tutorial for Beginners > Multithreading in C Sharp – Basics

Multithreading in C Sharp – Basics

Multithreading is made up with the conjunctions of two words Multi + threading = Multithreading.

Multithreading enables the multitasking & multiprocessing. It means you can run many program asynchronously or concurrently. Let me explain the meaning of threading because I am sure you all are aware of the English meaning of the word “Multi”. So thread is defined the execution path of your program. In terms of computer science thread is a smallest unit of processing.  You can divide a complex task into small sub task those are known as threads for example, OS manage different threads.

For example you can write something on word, simultaneously you can give instructions to print, and the same time you can chat with your friends. These all are possible by threads, you can write multiple threads to execute different actions in you program. In Multithreading helps to execute different process at the same time, it saves lots of time and increase the performance also. As we knows in .Net threads are running inside the App domain. Each app domain started a single thread but you can create other threads from the single threads.

In C#(.Net) you can use “using System.Threading” namespace to enable the multithreading. It contains important features such as

  • ThreadPooling  for mange the group of threads.
  • You can call timer class
  • By using Mutex class you can synchronize mutually exclusive threads, which helps us to remove the deadlock situations.
using System;
using System.Collections;
using System.Configuration;
using System.Threading;

 protected  void CallAnotherThread()
       Response.Write("Call Child Thread");

   protected void btnCallChildThread_Click(object sender, EventArgs e)
       ThreadStart ch1 = new ThreadStart(CallAnotherThread);
       Response.Write("Child threads, called");

       Thread th = new Thread(ch1);
       th.Start; // child thread start..

We can start thread by calling start method. In C# there are lots of methods are available few are below

  • Start (): Method is used to start the execution of threads.
  • Sleep (): Method is used to pause thread for some time. Once your thread started you can pause it for some time.
  • Abort (): Method is used to terminate the threads.
  • Suspended (): It is used for suspend a thread.
  • Resume (): It is used to resume a suspended threads.

Advantage of Threads:

  • It minimizes the use of system resources. Because they belongs to same process and share the same memory space.
  • Multithreading improves the performance of the system, because it execute your programs simultaneously.

Limitation of Threads

  • They increase the Deadlock condition, if it not handled smartly.

Sample Program

using System;
using System.Threading;

public class Test
   static void Main()
  ThreadStart job = new ThreadStart(ThreadJob);
  Thread thread = new Thread(job);

  for (int i=0; i < 5; i++)
  Console.WriteLine ("Main thread: {0}", i);

   static void ThreadJob()
  for (int i=0; i < 10; i++)
  Console.WriteLine ("Spawned thread: {0}", i);
  Thread.Sleep(500);// sleep method is used to pause

Post Comments Below

  1. No comments yet.
  1. No trackbacks yet.