在 C# 中检查一个线程是否属于托管线程池
csharpserver side programmingprogramming更新于 2025/9/11 19:50:00
要检查一个线程是否属于托管线程池,代码如下 −
示例
using System;
using System.Threading;
public class Demo {
public static void Main() {
Thread thread = new Thread(new ThreadStart(demo));
thread.Start();
}
public static void demo() {
Console.WriteLine("Thread belongs to managed thread pool? = "+Thread.CurrentThread.IsThreadPoolThread);
}
}
输出
这将产生以下输出 −
Thread belongs to managed thread pool? = False
示例
让我们看另一个例子 −
using System;
using System.Threading;
public class Demo {
public static void Main() {
ThreadPool.QueueUserWorkItem(new WaitCallback(demo));
}
public static void demo(object stateInfo) {
Console.WriteLine("Thread belongs to managed thread pool? = "+Thread.CurrentThread.IsThreadPoolThread);
}
}
输出
这将产生以下输出 −
Thread belongs to managed thread pool? = True

