Types of Threads in C#

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 :

  1. Foreground Thread
  2. Background Thread

Foreground Thread

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of foreground thread
using 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!!");
    }
}

chevron_right


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 tread doesn’t depend upon the life of the main thread. The thr thread only ends its process when it completes its assigned task.

Background Thread

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of Background thread
using 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);
    }
}

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.