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 »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.