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

CsharpServer Side ProgrammingProgramming

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
Published on 25-Sep-2020 14:36:28