要检查线程是否属于托管线程池,代码如下:
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.CurrentThread.IsThreadPoolThread); } }
输出结果
这将产生以下输出-
线程属于托管线程池吗? = 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.CurrentThread.IsThreadPoolThread); } }
输出结果
这将产生以下输出-
线程属于托管线程池吗? = True