Home  >  Article  >  Backend Development  >  How to get thread ID from thread in C#?

How to get thread ID from thread in C#?

王林
王林forward
2023-09-15 20:25:021524browse

How to get thread ID from thread in C#?

Thread is defined as the execution path of the program. Each thread defines a unique Process control. If your application involves complex and time-consuming operations operation, it is often helpful to set up different execution paths or threads, Each thread performs a specific task.

Threads are lightweight processes. A common example of thread usage is Modern operating systems implement concurrent programming. use Threads save wasted CPU cycles and increase application efficiency.

In C#, the System.Threading.Thread class is used to handle threads. It allows the creation and access of individual threads in multi-threaded applications. In a process, the first thread to execute is called the main thread.

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

Example

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);
      Console.ReadLine();
   }
}

Output

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

The above is the detailed content of How to get thread ID from thread in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:C# string propertiesNext article:C# string properties