Thread类负责在多线程编程中创建和管理线程。它提供了一个称为ManagedThreadId的属性来检查当前托管线程的唯一标识符。换句话说,线程的ManagedThreadId属性的值在其进程内唯一定义该线程。 ManagedThreadId属性的值不会随时间变化。
句法:
public int ManagedThreadId { get; }
返回值:该属性返回一个值,该值指示此托管线程的唯一标识符。此属性的返回类型为System.Int32 。
范例1:
// C# program to illustrate the
// use of ManagedThreadId property
using System;
using System.Threading;
public class GFG {
// Main Method
static public void Main()
{
Thread T;
// Get the reference of main Thread
// Using CurrentThread property
T = Thread.CurrentThread;
// Display the unique id of the main
// thread Using ManagedThreadId property
Console.WriteLine("The unique id of the main "+
"thread is: {0} ", T.ManagedThreadId);
}
}
输出:
The unique id of the main thread is: 1
范例2:
// C# program to illustrate the
// use of ManagedThreadId property
using System;
using System.Threading;
public class GFG {
// Main method
public static void Main()
{
// Creating and initializing threads
Thread thr1 = new Thread(new ThreadStart(job));
Thread thr2 = new Thread(new ThreadStart(job));
Thread thr3 = new Thread(new ThreadStart(job));
Console.WriteLine("ManagedThreadId of thread 1 "+
"is: {0}", thr1.ManagedThreadId);
Console.WriteLine("ManagedThreadId of thread 2 "+
"is: {0}", thr2.ManagedThreadId);
Console.WriteLine("ManagedThreadId of thread 3 "+
"is: {0}", thr3.ManagedThreadId);
// Running state
thr1.Start();
thr2.Start();
thr3.Start();
}
// Static method
public static void job()
{
Thread.Sleep(2000);
}
}
输出:
ManagedThreadId of thread 1 is: 3
ManagedThreadId of thread 2 is: 4
ManagedThreadId of thread 3 is: 5
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.managedthreadid?view=netframework-4.7.2