Thread.IsThreadPoolThread 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取指示线程是否属于托管线程池的值。
public:
property bool IsThreadPoolThread { bool get(); };
public bool IsThreadPoolThread { get; }
member this.IsThreadPoolThread : bool
Public ReadOnly Property IsThreadPoolThread As Boolean
属性值
如果此线程属于托管线程池,则为 true
;否则为 false
。
示例
下面的代码示例演示如何确定线程是否来自线程池。
using namespace System;
using namespace System::Threading;
ref class IsThreadPool
{
public:
static void ThreadMethod()
{
Console::WriteLine( "ThreadOne, executing ThreadMethod, "
"is {0}from the thread pool.", Thread::CurrentThread->IsThreadPoolThread ? (String^)"" : "not " );
}
static void WorkMethod( Object^ stateInfo )
{
Console::WriteLine( "ThreadTwo, executing WorkMethod, "
"is {0}from the thread pool.", Thread::CurrentThread->IsThreadPoolThread ? (String^)"" : "not " );
// Signal that this thread is finished.
dynamic_cast<AutoResetEvent^>(stateInfo)->Set();
}
};
int main()
{
AutoResetEvent^ autoEvent = gcnew AutoResetEvent( false );
Thread^ regularThread = gcnew Thread( gcnew ThreadStart( &IsThreadPool::ThreadMethod ) );
regularThread->Start();
ThreadPool::QueueUserWorkItem( gcnew WaitCallback( &IsThreadPool::WorkMethod ), autoEvent );
// Wait for foreground thread to end.
regularThread->Join();
// Wait for background thread to end.
autoEvent->WaitOne();
}
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();
}
}
Option Explicit
Option Strict
Imports System.Threading
Public Class IsThreadPool
<MTAThread> _
Shared Sub Main()
Dim autoEvent As New AutoResetEvent(False)
Dim regularThread As New Thread(AddressOf ThreadMethod)
regularThread.Start()
ThreadPool.QueueUserWorkItem(AddressOf WorkMethod, autoEvent)
' Wait for foreground thread to end.
regularThread.Join()
' Wait for background thread to end.
autoEvent.WaitOne()
End Sub
Shared Sub ThreadMethod()
Console.WriteLine("ThreadOne, executing ThreadMethod, " & _
"is from the thread pool? {0}", _
Thread.CurrentThread.IsThreadPoolThread)
End Sub
Shared Sub WorkMethod(stateInfo As Object)
Console.WriteLine("ThreadTwo, executing WorkMethod, " & _
"is from the thread pool? {0}", _
Thread.CurrentThread.IsThreadPoolThread)
' Signal that this thread is finished.
DirectCast(stateInfo, AutoResetEvent).Set()
End Sub
End Class
注解
有关详细信息,请参阅 托管线程池。