/*
* 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.
Program:
Output:
Explanation:
In the above program, we created a class ThreadEx that contains the Main() method. The Main() method contains the below code.
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.