Thread类负责在多线程编程中创建和管理线程。它提供了一个称为IsThreadPoolThread的属性,以检查线程是否属于托管线程池。
句法:
public bool IsThreadPoolThread { get; }
返回值:如果给定线程属于托管线程池,则此属性返回true。否则,返回false 。此属性的返回类型为System.Boolean 。
下面的程序说明了IsThreadPoolThread属性的用法:
范例1:
// C# program to illustrate the use
// of IsThreadPoolThread property
using System;
using System.Threading;
class GFG {
// Main Method
static public void Main()
{
Thread T;
// Get the reference of main Thread
// Using CurrentThread property
T = Thread.CurrentThread;
// Check if the main thread belongs to
// the managed thread pool or not
// Using IsThreadPoolThread property
Console.WriteLine("Is main thread belongs to Thread"+
" pool? : {0} ", T.IsThreadPoolThread);
}
}
输出:
Is main thread belongs to Thread pool? : False
范例2:
// C# program to illustrate the use
// of IsThreadPoolThread property
using System;
using System.Threading;
class GFG {
// Main method
public static void Main()
{
// Creating and initializing threads
Thread TR1 = new Thread(new ThreadStart(job));
ThreadPool.QueueUserWorkItem(new WaitCallback(job1));
TR1.Start();
}
// Static methods
public static void job()
{
Console.WriteLine("Is thread 1 belongs to thread pool: {0}",
Thread.CurrentThread.IsThreadPoolThread);
}
public static void job1(object stateInfo)
{
Console.WriteLine("Is thread 2 belongs to thread pool: {0}",
Thread.CurrentThread.IsThreadPoolThread);
}
}
输出:
Is thread 1 belongs to thread pool: False
Is thread 2 belongs to thread pool: True
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.isthreadpoolthread?view=netframework-4.7.2