Thread.IsThreadPoolThread 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出執行緒是否屬於 Managed 執行緒集區。
public:
property bool IsThreadPoolThread { bool get(); };
public bool IsThreadPoolThread { get; }
member this.IsThreadPoolThread : bool
Public ReadOnly Property IsThreadPoolThread As Boolean
屬性值
true
,表示這個執行緒屬於 Managed 執行緒集區,否則為 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
備註
如需詳細資訊,請參閱 managed 執行緒集區。