Export (0) Print
Expand All

Thread.IsThreadPoolThread Property

Gets a value indicating whether or not a thread belongs to the managed thread pool.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

public bool IsThreadPoolThread { get; }
/** @property */
public boolean get_IsThreadPoolThread ()

public function get IsThreadPoolThread () : boolean

Not applicable.

Property Value

true if this thread belongs to the managed thread pool; otherwise, false.

For more information see The Managed Thread Pool.

The following code example shows how to determine whether a thread is from the thread pool.

using System;
using System.Threading;

class IsThreadPool
{
    static void Main()
    {
        AutoResetEvent autoEvent = new AutoResetEvent(false);

        Thread regularThread = 
            new Thread(new ThreadStart(ThreadMethod));
        regularThread.Start();
        ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), 
            autoEvent);

        // Wait for foreground thread to end.
        regularThread.Join();

        // Wait for background thread to end.
        autoEvent.WaitOne();
    }

    static void ThreadMethod()
    {
        Console.WriteLine("ThreadOne, executing ThreadMethod, " +
            "is {0}from the thread pool.", 
            Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
    }

    static void WorkMethod(object stateInfo)
    {
        Console.WriteLine("ThreadTwo, executing WorkMethod, " +
            "is {0}from the thread pool.", 
            Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");

        // Signal that this thread is finished.
        ((AutoResetEvent)stateInfo).Set();
    }
}

import System.*;
import System.Threading.*;
import System.Threading.Thread;

class IsThreadPool
{
    public static void main(String[] args)
    {
        AutoResetEvent autoEvent = new AutoResetEvent(false);
        Thread regularThread = new Thread(new ThreadStart(ThreadMethod));

        regularThread.Start();
        ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), autoEvent);

        // Wait for foreground thread to end.
        regularThread.Join();

        // Wait for background thread to end.
        autoEvent.WaitOne();
    } //main

    static void ThreadMethod()
    {
        Console.WriteLine("ThreadOne, executing ThreadMethod, " 
            +  "is {0}from the thread pool.", 
            (Thread.get_CurrentThread().get_IsThreadPoolThread())
            ? "" : "not ");
    } //ThreadMethod

    static void WorkMethod(Object stateInfo)
    {
        Console.WriteLine("ThreadTwo, executing WorkMethod, " + 
            "is {0}from the thread pool.",
            (Thread.get_CurrentThread().get_IsThreadPoolThread()) 
            ? "" : "not ");

        // Signal that this thread is finished.
        ((AutoResetEvent)(stateInfo)).Set();
    } //WorkMethod
} //IsThreadPool

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft