Home »
.Net »
C# Programs
C# program to assign the name to the thread
Here, we are going to learn how to assign the name to the thread in C#?
Submitted by Nidhi, on August 16, 2020
Here we used Name property Thread class. The Name property is used to set or get the name of the thread.
Program:
/*
* Program to assign the name to the thread in C#.
*/
using System;
using System.Threading;
class ThreadEx
{
static void Main()
{
if (Thread.CurrentThread.Name == null)
{
Thread.CurrentThread.Name = "MyMainThread";
Console.WriteLine("Name of the thread assigned successfully");
}
else
{
Console.WriteLine("Name of thread already assigned");
}
}
}
Output:
Name of the thread assigned successfully
Press any key to continue . . .
Explanation:
In the above program, we created a class ThreadEx that contains the Main() method. The Main() method contains the below code.
if (Thread.CurrentThread.Name == null)
{
Thread.CurrentThread.Name = "MyMainThread";
Console.WriteLine("Name of the thread assigned successfully");
}
else
{
Console.WriteLine("Name of thread already assigned");
}
In the above code we checked if Name property contains "null" value then we assigned the MyMainThread name to the current thread otherwise we print "Name of thread already assigned" on the console screen.
C# Thread Programs »