A thread is a light-weight process within a process. In C#, a user is allowed to assign a name to the thread and also find the name of the current working thread by using the Thread.Name property of the Thread class.
Syntax :
public string Name { get; set; }
Here, the string contains the name of the thread or null if no name was assigned or set.
Important Points:
- The by default value of the Name property is null.
- The string given to the name property can include any Unicode Character.
- The name property of Thread class is write-once.
- If a set operation was requested, but the Name property has already set, then it will give
InvalidOperationException.
Example 1:
// C# program to illustrate the // concept of assigning names // to the thread and fetching // name of the current working thread using System; using System.Threading; class Geek { // Method of Geek class public void value() { // Fetching the name of // the current thread // Using Name property Thread thr = Thread.CurrentThread; Console.WriteLine("The name of the current "+ "thread is: " + thr.Name); } } // Driver class public class GFG { // Main Method static public void Main() { // Creating object of Geek class Geek obj = new Geek(); // Creating and initializing threads Thread thr1 = new Thread(obj.value); Thread thr2 = new Thread(obj.value); Thread thr3 = new Thread(obj.value); Thread thr4 = new Thread(obj.value); // Assigning the names of the threads // Using Name property thr1.Name = "Geeks1"; thr2.Name = "Geeks2"; thr3.Name = "Geeks3"; thr4.Name = "Geeks4"; thr1.Start(); thr2.Start(); thr3.Start(); thr4.Start(); } } |
chevron_right
filter_none
Output:
The name of the current thread is: Geeks2 The name of the current thread is: Geeks3 The name of the current thread is: Geeks4 The name of the current thread is: Geeks1
Example 2:
// C# program to illustrate the // concept of giving a name to thread using System; using System.Threading; class Name { static void Main() { // Check whether the thread // has already been named // to avoid InvalidOperationException if (Thread.CurrentThread.Name == null) { Thread.CurrentThread.Name = "MyThread"; Console.WriteLine("The name of the thread is MyThread"); } else { Console.WriteLine("Unable to name the given thread"); } } } |
chevron_right
filter_none
Output:
The name of the thread is MyThread
Reference:
Recommended Posts:
- Suspending the current thread for the specified amount of time in C#
- C# | How to check current state of a thread
- C# | Getting the unique identifier for the current managed thread
- How to check whether a thread is a background thread or not in C#
- C# | Check if a thread belongs to managed thread pool or not
- C# | Getting the type of the current instance
- How to display Current Date/Time in the DateTimePicker in C#?
- Lifecycle and States of a Thread in C#
- 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.

