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
Main thread vs child thread in C#
In C#, every application starts with a main thread that executes the Main method. Additional threads, called child threads, can be created to perform concurrent operations alongside the main thread.
Main Thread
The main thread is automatically created when a C# program starts execution. It is the first thread to run in a process and is responsible for executing the Main method. The main thread can create and manage other threads during program execution.
Child Thread
A child thread is any thread created from the main thread using the Thread class or other threading mechanisms. Child threads run concurrently with the main thread and can perform background tasks without blocking the main thread's execution.
Working with Main Thread
Example
using System;
using System.Threading;
class Program {
static void Main(string[] args) {
Thread mainThread = Thread.CurrentThread;
mainThread.Name = "MainThread";
Console.WriteLine("Thread Name: {0}", mainThread.Name);
Console.WriteLine("Thread ID: {0}", mainThread.ManagedThreadId);
Console.WriteLine("Is Background Thread: {0}", mainThread.IsBackground);
Console.WriteLine("Thread State: {0}", mainThread.ThreadState);
}
}
The output of the above code is −
Thread Name: MainThread Thread ID: 1 Is Background Thread: False Thread State: Running
Creating Child Threads
Example
using System;
using System.Threading;
class Program {
static void Main(string[] args) {
Console.WriteLine("Main thread started");
Thread childThread1 = new Thread(ChildMethod1);
Thread childThread2 = new Thread(ChildMethod2);
childThread1.Name = "Child-1";
childThread2.Name = "Child-2";
childThread1.Start();
childThread2.Start();
childThread1.Join();
childThread2.Join();
Console.WriteLine("Main thread finished");
}
static void ChildMethod1() {
for (int i = 1; i <= 3; i++) {
Console.WriteLine("{0}: Count {1}", Thread.CurrentThread.Name, i);
Thread.Sleep(1000);
}
}
static void ChildMethod2() {
for (int i = 1; i <= 3; i++) {
Console.WriteLine("{0}: Message {1}", Thread.CurrentThread.Name, i);
Thread.Sleep(800);
}
}
}
The output of the above code is −
Main thread started Child-1: Count 1 Child-2: Message 1 Child-2: Message 2 Child-1: Count 2 Child-2: Message 3 Child-1: Count 3 Main thread finished
Key Differences
| Main Thread | Child Thread |
|---|---|
| Automatically created by the runtime | Explicitly created using Thread class |
| Foreground thread by default | Foreground thread by default (can be set to background) |
| Application terminates when main thread ends | Application waits for foreground child threads to complete |
| Has the lowest thread ID (usually 1) | Has higher thread IDs assigned by the system |
Thread Lifecycle Management
Example
using System;
using System.Threading;
class Program {
static void Main(string[] args) {
Console.WriteLine("Main thread: {0}", Thread.CurrentThread.ManagedThreadId);
Thread backgroundThread = new Thread(BackgroundWork);
backgroundThread.IsBackground = true;
backgroundThread.Name = "Background Worker";
Thread foregroundThread = new Thread(ForegroundWork);
foregroundThread.Name = "Foreground Worker";
backgroundThread.Start();
foregroundThread.Start();
Console.WriteLine("Main thread ending...");
}
static void BackgroundWork() {
for (int i = 1; i <= 5; i++) {
Console.WriteLine("Background thread working... {0}", i);
Thread.Sleep(1000);
}
}
static void ForegroundWork() {
for (int i = 1; i <= 2; i++) {
Console.WriteLine("Foreground thread working... {0}", i);
Thread.Sleep(500);
}
}
}
The output of the above code is −
Main thread: 1 Main thread ending... Background thread working... 1 Foreground thread working... 1 Foreground thread working... 2 Background thread working... 2
Conclusion
The main thread is the primary execution thread that starts with your application, while child threads are additional threads you create for concurrent processing. Understanding the difference helps you design applications that can perform multiple tasks simultaneously while managing thread lifecycle properly.
