A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as ManagedThreadId to check the unique identifier for the current managed thread. Or in other words, the value of ManagedThreadId property of a thread defines uniquely that thread within its process. The value of the ManagedThreadId property does not vary according to time.
Syntax:
public int ManagedThreadId { get; }
Return Value: This property returns a value that indicates a unique identifier for this managed thread. The return type of this property is System.Int32.
Example 1:
// C# program to illustrate the // use of ManagedThreadId property using System; using System.Threading; public class GFG { // Main Method static public void Main() { Thread T; // Get the reference of main Thread // Using CurrentThread property T = Thread.CurrentThread; // Display the unique id of the main // thread Using ManagedThreadId property Console.WriteLine("The unique id of the main "+ "thread is: {0} ", T.ManagedThreadId); } } |
Output:
The unique id of the main thread is: 1
Example 2:
// C# program to illustrate the // use of ManagedThreadId property using System; using System.Threading; public class GFG { // Main method public static void Main() { // Creating and initializing threads Thread thr1 = new Thread(new ThreadStart(job)); Thread thr2 = new Thread(new ThreadStart(job)); Thread thr3 = new Thread(new ThreadStart(job)); Console.WriteLine("ManagedThreadId of thread 1 "+ "is: {0}", thr1.ManagedThreadId); Console.WriteLine("ManagedThreadId of thread 2 "+ "is: {0}", thr2.ManagedThreadId); Console.WriteLine("ManagedThreadId of thread 3 "+ "is: {0}", thr3.ManagedThreadId); // Running state thr1.Start(); thr2.Start(); thr3.Start(); } // Static method public static void job() { Thread.Sleep(2000); } } |
Output:
ManagedThreadId of thread 1 is: 3 ManagedThreadId of thread 2 is: 4 ManagedThreadId of thread 3 is: 5
Reference:
Recommended Posts:
- C# | Check if a thread belongs to managed thread pool or not
- Naming a thread and fetching name of current thread in C#
- Difference between Managed and Unmanaged code in .NET
- Managed code and Unmanaged code in .NET
- How to check whether a thread is a background thread or not in C#
- Suspending the current thread for the specified amount of time in C#
- C# | How to check current state of a thread
- C# | Getting the type of the current instance
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- C# | Check if StringCollection is synchronized (thread safe)
- Thread.CurrentThread Property in C#
- C# | Check if the BitArray is synchronized (thread safe)
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Check if ListDictionary is synchronized (thread safe)
- C# | Check if HybridDictionary is Synchronized (thread safe)
- C# | Check if Hashtable is synchronized (thread safe)
- C# | Check if ArrayList is Synchronized (thread safe)
- C# | Check if an array is synchronized (thread safe) or not
- C# | Creating a synchronized (thread-safe) wrapper for the Hashtable
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList
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.

