คุณสมบัติ Thread.CurrentThread ใน C# ใช้ดึงอ็อบเจ็กต์ Thread ที่แทนเธรดที่กำลังทำงานอยู่ในขณะนี้
ไวยากรณ์ (Syntax)
public static Thread CurrentThread { get; }
ตัวอย่างที่ 1: ดูข้อมูลเธรดปัจจุบันและ ThreadPool
using System;
using System.Threading;
public class Demo
{
public static void Main()
{
Thread thread = new Thread(new ThreadStart(Demo1));
ThreadPool.QueueUserWorkItem(new WaitCallback(Demo2));
thread = Thread.CurrentThread;
thread.Name = "Demo Thread";
Console.WriteLine("Current running Thread = " + thread.Name);
Console.WriteLine("Current state of Thread = " + thread.ThreadState);
Console.WriteLine("ManagedThreadId = " + thread.ManagedThreadId);
}
public static void Demo1()
{
Thread.Sleep(2000);
}
public static void Demo2(object stateInfo)
{
Console.WriteLine("Thread belongs to managed thread pool? = " + Thread.CurrentThread.IsThreadPoolThread);
}
}
ผลลัพธ์
Current running Thread = Demo Thread Current state of Thread = Running ManagedThreadId = 20 Thread belongs to managed thread pool? = True
ตัวอย่างที่ 2: ดู ManagedThreadId เพิ่มเติม
using System;
using System.Threading;
public class Demo
{
public static void Main()
{
Thread thread = new Thread(new ThreadStart(Demo1));
ThreadPool.QueueUserWorkItem(new WaitCallback(Demo2));
thread = Thread.CurrentThread;
thread.Name = "Demo Thread";
Console.WriteLine("Current running Thread = " + thread.Name);
Console.WriteLine("Current state of Thread = " + thread.ThreadState);
Console.WriteLine("ManagedThreadId = " + thread.ManagedThreadId);
Console.WriteLine("Thread Id: {0}", Thread.CurrentThread.ManagedThreadId);
}
public static void Demo1()
{
Thread.Sleep(2000);
}
public static void Demo2(object stateInfo)
{
Console.WriteLine("Thread belongs to managed thread pool? = " + Thread.CurrentThread.IsThreadPoolThread);
}
}
ผลลัพธ์
Current running Thread = Demo Thread Current state of Thread = Running ManagedThreadId = 29 Thread Id: 29 Thread belongs to managed thread pool? = True
สรุป
Thread.CurrentThreadคืนอ็อบเจ็กต์Threadของเธรดที่กำลัง execute โค้ดอยู่- คุณสมบัติหลักของ Thread:
Name — ชื่อเธรด (ตั้งได้ เพื่อ debugging)ManagedThreadId — ID เฉพาะของ managed threadThreadState — สถานะ (Running, WaitSleepJoin, Stopped, Aborted, etc.)IsThreadPoolThread — true หากเป็นเธรดจาก ThreadPoolIsBackground — true = background thread (ไม่กัน process จบ)Priority — ลำดับความสำคัญ (Lowest..Highest)CurrentCulture / CurrentUICulture — วัฒนธรรมสำหรับ formatting/resource
- ThreadPool.QueueUserWorkItem ใช้เธรดจาก pool —
IsThreadPoolThread = true - ใน ASP.NET Core / modern .NET: หลีกเลี่ยงการจัดการ Thread ด้วยมือ — ใช้
async/await,Task.Run,Parallel,Channelsแทน
คุณสมบัติ Thread ที่น่าสนใจ
| คุณสมบัติ | คำอธิบาย |
|---|---|
| CurrentThread | Static property - เธรดปัจจุบัน |
| ManagedThreadId | ID เฉพาะของ managed thread |
| Name | ชื่อเธรด (สำหรับ debug) |
| ThreadState | สถานะเธรด (bit flags) |
| IsAlive | เธรดทำงานอยู่หรือไม่ |
| IsBackground | Background thread หรือไม่ |
| IsThreadPoolThread | มาจาก ThreadPool หรือไม่ |
| Priority | ลำดับความสำคัญ |