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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Is main thread belongs to Thread pool? : False 

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Is thread 1 belongs to thread pool: False
Is thread 2 belongs to thread pool: True

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.