Open In App

Types of Threads in C#

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In C#, there are two types of threads: foreground threads and background threads.

  1. Foreground Threads: These threads are created using the Thread class in C# and are considered to be the main threads of an application. They prevent the application from terminating until all foreground threads have completed their tasks. A foreground thread can be created by calling the Thread.Start() method.
  2. Background Threads: These threads are also created using the Thread class in C#, but they are considered to be the secondary threads of an application. They do not prevent the application from terminating, even if they are still running. A background thread can be created by calling the Thread.Start() method and then setting the IsBackground property of the thread to true before starting it.
  3. When an application starts, it automatically creates a foreground thread, which is the main thread of the application. All the threads are background thread by default. However, you can change the type of a thread by setting the IsBackground property of the thread.
  4. Foreground threads are useful when you need to perform a task that is critical to the operation of the application and must be completed before the application can terminate. For example, a foreground thread might be used to update the user interface or to perform important calculations.
  5. Background threads are useful when you need to perform a task that is not critical to the operation of the application and can be terminated if the application needs to shut down. For example, a background thread might be used to download a large file or to perform periodic updates to a database.

It is important to note that a background thread can be terminated at any time if the application needs to shut down. Therefore, you should be careful when using background threads and ensure that they are not performing critical tasks that could cause the application to malfunction if they are terminated abruptly.

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 :
 

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:
 

CSharp




// 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!!");
    }
}


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.
 

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:
 

CSharp




// 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);
    }
}


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.
 



Last Updated : 19 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads