Multithreading In C# .Net

Introduction

If you have a program that executes from top to bottom, it will not be responsive and feasible to build complex applications. So, the .NET Framework offers some classes to create complex applications.

What is threading?

In short, thread is like a virtualized CPU which will help to develop complex applications.

Understanding threading

Suppose, you have a computer which only has one CPU capable of executing only one operation at a time. And, your application has a complex operation. So, in this situation, your application will take too much time. This means that the whole machine will freeze and appear unresponsive. So your application performance will decrease.

For protecting the performance, we will be multithreading in C#.NET. So, we will divide our program into different parts using threading. And you know every application run its own process in Windows. So every process will run in its own thread.

Thread Class

Thread class can be found in System.Threading namespace, using this class you can create a new thread and can manage your program like property, status etc. of your thread.

Example

The following code shows how to create thread in C#.NET

  1. class Program  
  2.     {  
  3.         private static void loopTask()  
  4.         {  
  5.             for(int x = 0; x<10; x++)  
  6.             {  
  7.                 Console.WriteLine("X => {0}", x);  
  8.                 Thread.Sleep(1000);  
  9.             }  
  10.         }  
  11.   
  12.         static void Main(string[] args)  
  13.         {  
  14.             ThreadStart thread = new ThreadStart(loopTask);  
  15.             Thread myThread = new Thread(thread);  
  16.               
  17.             myThread.Start();  
  18.   
  19.             for (int y = 0; y < 4; y++)  
  20.             {  
  21.                 Console.WriteLine("Main thread.");  
  22.                 Thread.Sleep(1000);  
  23.             }  
  24.   
  25.             Console.ReadKey();  
  26.         }  

Explanation

You may observe that, I created a new Local Variable thread of ThreadStart delegate and pass loopTask as a parameter to execute it. This loopTask Function has a loop. And we create a new object myThread from Thread Class and pass Local Variable thread to Thread Constructor. And start the Thread using myThread.Start(); and Thread.Sleep(2000); will pause for 2000 milliseconds.

And finally the result will be

C#

This code also can be written in a more simple way like this.

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             new Thread(new ThreadStart(() => {  
  6.                 for (int x = 0; x < 10; x++)  
  7.                 {  
  8.                     Console.WriteLine("X => {0}", x);  
  9.                     Thread.Sleep(1000);  
  10.                 }  
  11.             })).Start();  
  12.   
  13.             Console.ReadKey();  
  14.         }  
  15.     }  

In the above code, we are using lambda expression ( => ) for initialization.

Passing Value as Parameter

The Thread constructor has another overload that takes an instance of a ParameterizedThreadStart delegate. It can be used if you want to pass some data through the start method of your thread to your method,

  1. class Program  
  2.     {  
  3.         private static void loopTask(object value)  
  4.         {  
  5.             for(int x = 0; x<10; x++)  
  6.             {  
  7.                 Console.WriteLine("X => {0} And Parameter Value is => {1}", x, value);  
  8.                 Thread.Sleep(1000);  
  9.             }  
  10.         }  
  11.   
  12.         static void Main(string[] args)  
  13.         {  
  14.             Thread myThread = new Thread( new ParameterizedThreadStart(loopTask));  
  15.               
  16.             myThread.Start("Kashif");  
  17.   
  18.             Console.ReadKey();  
  19.         }  
  20.     }  

In this case, the value 5 is passed to the loopTask as an object. You can cast it to the expected type.