Thread.IsThreadPoolThread Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan nilai yang menunjukkan apakah utas milik kumpulan utas terkelola atau tidak.
public:
property bool IsThreadPoolThread { bool get(); };
public bool IsThreadPoolThread { get; }
member this.IsThreadPoolThread : bool
Public ReadOnly Property IsThreadPoolThread As Boolean
Nilai Properti
true jika utas ini milik kumpulan utas terkelola; jika tidak, false.
Contoh
Contoh kode berikut menunjukkan cara menentukan apakah utas berasal dari kumpulan utas.
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();
}
}
open System.Threading
let threadMethod () =
printfn
$"""ThreadOne, executing ThreadMethod, is {if Thread.CurrentThread.IsThreadPoolThread then
""
else
"not "}from the thread pool."""
let workMethod stateInfo =
printfn
$"""ThreadTwo, executing WorkMethod, is {if Thread.CurrentThread.IsThreadPoolThread then
""
else
"not "}from the thread pool."""
// Signal that this thread is finished.
(unbox<AutoResetEvent> stateInfo).Set() |> ignore
let autoEvent = new AutoResetEvent false
let regularThread = Thread threadMethod
regularThread.Start()
ThreadPool.QueueUserWorkItem(workMethod, autoEvent) |> ignore
// Wait for foreground thread to end.
regularThread.Join()
// Wait for background thread to end.
autoEvent.WaitOne() |> ignore
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
Keterangan
Untuk informasi selengkapnya, lihat Kumpulan utas terkelola.