A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as CurrentThread to check the current running thread. Or in other words, the value of this property indicates the current running thread.
Syntax: public static Thread CurrentThread { get; }
Return Value: This property returns a thread that represent the current running thread.
Below programs illustrate the use of CurrentThread property:
.
Example 1:
// C# program to illustrate the // use of CurrentThread property using System; using System.Threading; class GFG { // Main Method static public void Main() { Thread thr; // Get the reference of main Thread // Using CurrentThread property thr = Thread.CurrentThread; thr.Name = "Main thread"; Console.WriteLine("Name of current running "+ "thread: {0}", thr.Name); } } |
Output:
Name of current running thread: Main thread
Example 2:
// C# program to illustrate the // use of CurrentThread property using System; using System.Threading; class GFG { // Display the id of each thread // Using CurrentThread and // ManagedThreadId properties public static void Myjob() { Console.WriteLine("Thread Id: {0}", Thread.CurrentThread.ManagedThreadId); } // Main method static public void Main() { // Creating multiple threads ThreadStart value = new ThreadStart(Myjob); for (int q = 1; q <= 7; ++q) { Thread mythread = new Thread(value); mythread.Start(); } } } |
Output:
Thread Id: 3 Thread Id: 8 Thread Id: 9 Thread Id: 6 Thread Id: 5 Thread Id: 7 Thread Id: 4
Reference:
Recommended Posts:
- Uri.IsLoopback Property in C# with Example
- C# | SortedDictionary.Item[] Property
- C# | SortedDictionary.Count Property
- C# | SortedDictionary.Keys Property
- C# | SortedDictionary.Values Property
- Queue.Count Property in C#
- Queue.IsSynchronized Property in C#
- Stack.Count Property in C#
- Stack.IsSynchronized Property in C#
- Console.KeyAvailable() Property in C#
- C# | LongLength property of an Array
- How to use AutoCheck property of CheckBox in C#?
- How to use AcceptsReturn Property of TextBox in C#?
- StringBuilder.Chars[] Property in C#
- C# | Dictionary.Item[] Property
- C# | Dictionary.Values Property
- C# | Dictionary.Keys Property
- C# | Dictionary.Count Property
- Uri.Fragment Property in C# with Examples
- Uri.DnsSafeHost Property in C# with Examples
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.
Improved By : Akanksha_Rai

