Home > Backend Development > C#.Net Tutorial > How to get thread ID from thread in C#?

How to get thread ID from thread in C#?

王林
Release: 2023-09-15 20:25:02
forward
1685 people have browsed it

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();
   }
}
Copy after login

Output

Name of current running thread: Main thread
Id of current running thread: 1
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template