Need of Thread in C# – Learn about process and thread behaviours with explanation and example programs.
Before going to learn about thread concept, we will learn about how a process works?
Process
- Program under execution with the operating system instructions is called a process.
- Each process assigned with separate copy of memory for its execution with unique process-id.
For Example your OS is a single processing system i.e it can perform only one task at a time.
Suppose you would like to listen music while you are programming in the system. But your OS is a single processing system , it can allow only one task at a time. Now either you can listen the music or you can do programming but not both.
C# example program to show single processing system execution.
c# code
class Program
{
static void Main(string[] args)
{
Programming();
ListeningMusic();
}
static void Programming()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Executing Program - " + i);
}
}
static void ListeningMusic()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Listening Music - " + i);
}
}
}
Output
Executing Program – 0
Executing Program – 1
Executing Program – 2
Executing Program – 3
Executing Program – 4
Listening Music – 0
Listening Music – 1
Listening Music – 2
Listening Music – 3
Listening Music – 4
In the above example, we can observe that ListeningMusic() method execution is done only after completion of Programming() method execution as per the discussion of single processing system.
To do both tasks at a time (programming and listening music ) your OS system needs to support multi processing i.e More Than one process execution at a time. We can achieve this by using threads.
Thread
- It is a light weight process that executes the code independently.
- Thread able to run the program simultaneously with other threads.
- A process has at least one thread which is commonly called as main thread, that actually executes the application code.
A single process can have multiple threads.
The execution of main thread will take care by CLR and O.S. This main thread will create child threads using system.Threading namespace.
C# Program to show how multiprocessing is achieved using threads.
c# code
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
//Create object of a thread class and pass
//a function as a parameter to it which is called by
//this thread.
Thread lm = new Thread(ListeningMusic);
////Start a new thread
lm.Start();
// Simultaneously, perform some task in main thread also.
Programming();
}
static void Programming()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Executing Program - " + i);
}
Console.WriteLine("Programming Task completed");
}
static void ListeningMusic()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Listening Music - " + i);
}
Console.WriteLine("Listening Music task is completed");
}
}
Output
Listening Music – 0
Executing Program – 0
Executing Program – 1
Listening Music – 1
Listening Music – 2
Listening Music – 3
Listening Music – 4
Listening Music task is completed
Executing Program – 2
Executing Program – 3
Executing Program – 4
Programming Task completed
In the above Example we can observe that , the programmer executing programs and listening music simultaneously.