C# - How to Print the Name of Current Thread?

Here, we are going to learn how to print the name of the current thread in C#? By Nidhi Last updated : March 29, 2023

Getting name of current thread

To get the name of the current thread, we created an object of using Thread.CurrentThread property then we assigned the thread name and then printed thread information with the help of Name property.

C# program to print the name of the current thread

/*
 * 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 »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.