Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Background and foreground thread in C#
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. In C#, threads are classified as either foreground threads or background threads based on their behavior when the main application terminates.
The key difference is that foreground threads keep the application alive, while background threads are automatically terminated when all foreground threads finish execution.
Thread Types Comparison
| Foreground Thread | Background Thread |
|---|---|
| Keeps the application running until it completes | Terminated automatically when all foreground threads end |
| Default thread type (IsBackground = false) | Must be explicitly set (IsBackground = true) |
| Application waits for completion | Application does not wait for completion |
Foreground Threads
Foreground threads continue to run until the last foreground thread is terminated. When all the foreground threads are stopped, the application is closed. The default threads that are created are foreground threads.
Example
using System;
using System.Threading;
class Program {
static void Main() {
Thread foregroundThread = new Thread(ForegroundWork);
Console.WriteLine("Foreground thread IsBackground: " + foregroundThread.IsBackground);
foregroundThread.Start();
Console.WriteLine("Main thread ending...");
}
static void ForegroundWork() {
Console.WriteLine("Foreground thread started");
Thread.Sleep(3000);
Console.WriteLine("Foreground thread completed");
}
}
The output of the above code is −
Foreground thread IsBackground: False Main thread ending... Foreground thread started Foreground thread completed
Background Threads
When the foreground threads will close, the background threads will be terminated. The property used for background thread is IsBackground that gets or sets a value indicating whether a thread is a background thread. The default value of this property would be false because the default threads created are foreground threads.
Syntax
To create a background thread −
Thread bgThread = new Thread(ThreadStart); bgThread.IsBackground = true; bgThread.Start();
Example
using System;
using System.Threading;
class Program {
static void Main() {
Thread backgroundThread = new Thread(BackgroundWork);
backgroundThread.IsBackground = true;
Console.WriteLine("Background thread IsBackground: " + backgroundThread.IsBackground);
backgroundThread.Start();
Thread.Sleep(1000);
Console.WriteLine("Main thread ending...");
}
static void BackgroundWork() {
Console.WriteLine("Background thread started");
for (int i = 1; i <= 10; i++) {
Console.WriteLine("Background working: " + i);
Thread.Sleep(500);
}
Console.WriteLine("Background thread would complete here");
}
}
The output of the above code is −
Background thread IsBackground: True Background thread started Background working: 1 Background working: 2 Main thread ending...
Comparing Both Thread Types
Example
using System;
using System.Threading;
class Program {
static void Main() {
Thread foreground = new Thread(DoWork);
Thread background = new Thread(DoWork);
background.IsBackground = true;
Console.WriteLine("Starting both threads...");
foreground.Start();
background.Start();
Console.WriteLine("Main thread finished");
}
static void DoWork() {
string threadType = Thread.CurrentThread.IsBackground ? "Background" : "Foreground";
for (int i = 1; i <= 5; i++) {
Console.WriteLine($"{threadType} thread: {i}");
Thread.Sleep(500);
}
Console.WriteLine($"{threadType} thread completed");
}
}
The output of the above code is −
Starting both threads... Main thread finished Foreground thread: 1 Background thread: 1 Foreground thread: 2 Background thread: 2 Foreground thread: 3 Background thread: 3 Foreground thread: 4 Background thread: 4 Foreground thread: 5 Background thread: 5 Foreground thread completed
Conclusion
Background and foreground threads serve different purposes in C# applications. Foreground threads keep the application running until completion, while background threads are automatically terminated when all foreground threads finish. Use background threads for tasks that should not prevent application shutdown, such as logging or monitoring operations.
