C# program to print the name of the current thread

Name of the current thread in C#: Here, we are going to learn how to print the name of the current thread in C#?
Submitted by Nidhi, on August 16, 2020

To solve the above problem, here we created using Thread.CurrentThread property then we assign thread name and then print thread information.

Program:

/*
 * Program to print the Name of the Current Thread
 */
using System;
using System.Threading;

class MyThreadClass
{
    static void Main(string[] args)
    {
        Thread t = Thread.CurrentThread;
        t.Name = "MyNewThread";

        Console.WriteLine("Thread information:");
        Console.WriteLine("\tName of the thread: "+t.Name);
        Console.WriteLine("\tStatus of thread  : "+(t.IsAlive==true?"Alive":"Not Alive"));
    } 
}

Output:

Thread information:
        Name of the thread: MyNewThread
        Status of thread  : Alive
Press any key to continue . . .

Explanation:

In the above program, we created a class MyThreadClass that contains the Main() method to start the execution of the program. In the Main() method, we created using Thread.CurrentThread property then we assigned thread name and then print thread information.

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.