Q:

C# program to assign the name to the thread

0

C# program to assign the name to the thread

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now