C# | Check if a thread belongs to managed thread pool or not
A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as IsThreadPoolThread to check if the thread belongs to the managed thread pool or not.
Syntax:
public bool IsThreadPoolThread { get; }
Return Value: This property returns true if the given thread belongs to the managed thread pool. Otherwise, return false. The return type of this property is System.Boolean.
Below programs illustrate the use of IsThreadPoolThread property:
Example 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); } } |
Output:
Is main thread belongs to Thread pool? : False
Example 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); } } |
Output:
Is thread 1 belongs to thread pool: False Is thread 2 belongs to thread pool: True
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread.isthreadpoolthread?view=netframework-4.7.2