How to get the thread ID from a thread in C#?

A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.

Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.

In C#, the System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads in a multithreaded application. The first thread to be executed in a process is called the main thread.

When a C# program starts execution, the main thread is automatically created. The threads created using the Thread class are called the child threads of the main thread. You can access a thread using the CurrentThread property of the Thread class.


class Program{
   public static void Main(){
      Thread thr;
      thr = Thread.CurrentThread;
      thr.Name = "Main thread";
      Console.WriteLine("Name of current running " + "thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Id of current running " + "thread: {0}", Thread.CurrentThread.ManagedThreadId);


Name of current running thread: Main thread
Id of current running thread: 1

Updated on: 25-Sep-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started