Types of Threads in C# - GeeksforGeeks

15 Sep, 2021
Multi-threading is the most useful feature of C# which allows concurrent programming of two or more parts of the program for maximizing the utilization of the CPU. Each part of a program is called Thread. So, in other words, threads are lightweight processes within a process. C# supports two types of threads are as follows :
 
A thread which keeps on running to complete its work even if the Main thread leaves its process, this type of thread is known as foreground thread. Foreground thread does not care whether the main thread is alive or not, it completes only when it finishes its assigned work. Or in other words, the life of the foreground thread does not depend upon the main thread.Example: 
CSharp
// C# program to illustrate the// concept of foreground threadusing System;using System.Threading; class GFG {     // Main method    static void Main(string[] args)    {         // Creating and initializing thread        Thread thr = new Thread(mythread);        thr.Start();        Console.WriteLine("Main Thread Ends!!");    }     // Static method    static void mythread()    {        for (int c = 0; c <= 3; c++) {             Console.WriteLine("mythread is in progress!!");            Thread.Sleep(1000);        }        Console.WriteLine("mythread ends!!");    }}
Output:  
Main Thread Ends!!
mythread is in progress!!
mythread is in progress!!
mythread is in progress!!
mythread is in progress!!
mythread ends!!
Explanation: In the above example, thr thread runs after main thread ended. So, the life of thr thread doesn’t depend upon the life of the main thread. The thr thread only ends its process when it completes its assigned task. 
A thread which leaves its process when the Main method leaves its process, these types of the thread are known as the background threads. Or in other words, the life of the background thread depends upon the life of the main thread. If the main thread finishes its process, then background thread also ends its process. Note: If you want to use a background thread in your program, then set the value of IsBackground property of the thread to true.Example: 
CSharp
// C# program to illustrate the// concept of Background threadusing System;using System.Threading; class GFG {     // Main method    static void Main(string[] args)    {        // Creating and initializing thread        Thread thr = new Thread(mythread);         // Name of the thread is Mythread        thr.Name = "Mythread";        thr.Start();         // IsBackground is the property of Thread        // which allows thread to run in the background        thr.IsBackground = true;         Console.WriteLine("Main Thread Ends!!");    }     // Static method    static void mythread()    {         // Display the name of the        // current working thread        Console.WriteLine("In progress thread is: {0}",                            Thread.CurrentThread.Name);         Thread.Sleep(2000);         Console.WriteLine("Completed thread is: {0}",                          Thread.CurrentThread.Name);    }}
Output:  
In progress thread is: Mythread
Main Thread Ends!!
Explanation: In the above example, IsBackground property of Thread class is used to set the thr thread as a background thread by making the value of IsBackground true. If you set the value of IsBackground false, then the given thread behaves as a foreground Thread. Now, the process of the thr thread ends when the process of the main thread ends. 
adnanirshad158
CSharp Multithreading
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Delegates
C# | Method Overriding
C# | Abstract Classes
Difference between Ref and Out keywords in C#
Extension Method in C#
C# | Class and Object
C# | Constructors
C# | String.IndexOf( ) Method | Set - 1
C# | Replace() Method