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
How to get the thread ID from a thread in C#?
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.
In C#, the System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads in a multithreaded application. The first thread to be executed in a process is called the main thread.
When a C# program starts execution, the main thread is automatically created. The threads created using the Thread class are called the child threads of the main thread. You can access a thread using the CurrentThread property of the Thread class.
Syntax
To get the thread ID from a thread, use the ManagedThreadId property −
int threadId = Thread.CurrentThread.ManagedThreadId;
To get a reference to the current thread −
Thread currentThread = Thread.CurrentThread;
Using CurrentThread Property
The Thread.CurrentThread property provides access to the currently executing thread. You can retrieve both the thread ID and other properties like the thread name −
using System;
using System.Threading;
class Program {
public static void Main() {
Thread thr;
thr = Thread.CurrentThread;
thr.Name = "Main thread";
Console.WriteLine("Name of current running thread: {0}", Thread.CurrentThread.Name);
Console.WriteLine("Id of current running thread: {0}", Thread.CurrentThread.ManagedThreadId);
}
}
The output of the above code is −
Name of current running thread: Main thread Id of current running thread: 1
Using Thread ID with Multiple Threads
When working with multiple threads, each thread gets a unique managed thread ID. Here's an example showing different thread IDs −
using System;
using System.Threading;
class Program {
public static void Main() {
Console.WriteLine("Main thread ID: {0}", Thread.CurrentThread.ManagedThreadId);
Thread t1 = new Thread(ShowThreadInfo);
Thread t2 = new Thread(ShowThreadInfo);
t1.Name = "Worker Thread 1";
t2.Name = "Worker Thread 2";
t1.Start();
t2.Start();
t1.Join();
t2.Join();
}
public static void ShowThreadInfo() {
Console.WriteLine("Thread Name: {0}, ID: {1}",
Thread.CurrentThread.Name,
Thread.CurrentThread.ManagedThreadId);
}
}
The output of the above code is −
Main thread ID: 1 Thread Name: Worker Thread 1, ID: 2 Thread Name: Worker Thread 2, ID: 3
Getting Thread ID from Thread Object
You can also get the thread ID directly from a Thread object without it being the current thread −
using System;
using System.Threading;
class Program {
public static void Main() {
Thread mainThread = Thread.CurrentThread;
Console.WriteLine("Main thread ID: {0}", mainThread.ManagedThreadId);
Thread workerThread = new Thread(DoWork);
Console.WriteLine("Worker thread ID before start: {0}", workerThread.ManagedThreadId);
workerThread.Start();
workerThread.Join();
}
public static void DoWork() {
Console.WriteLine("Inside worker thread, ID: {0}", Thread.CurrentThread.ManagedThreadId);
}
}
The output of the above code is −
Main thread ID: 1 Worker thread ID before start: 2 Inside worker thread, ID: 2
Key Properties
| Property | Description |
|---|---|
| ManagedThreadId | Gets the managed thread ID (unique integer for each thread) |
| Name | Gets or sets the name of the thread |
| IsAlive | Gets a value indicating whether the thread is alive |
| ThreadState | Gets the current state of the thread |
Conclusion
To get the thread ID from a thread in C#, use the ManagedThreadId property of the Thread class. You can access this property through Thread.CurrentThread.ManagedThreadId for the current thread or directly from any Thread object. Each thread in your application gets a unique managed thread ID that helps identify and manage different execution paths.
