Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

คุณสมบัติ Thread.CurrentThread ใน C#

คุณสมบัติ 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 thread
    • ThreadState — สถานะ (Running, WaitSleepJoin, Stopped, Aborted, etc.)
    • IsThreadPoolThread — true หากเป็นเธรดจาก ThreadPool
    • IsBackground — 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 ที่น่าสนใจ

คุณสมบัติคำอธิบาย
CurrentThreadStatic property - เธรดปัจจุบัน
ManagedThreadIdID เฉพาะของ managed thread
Nameชื่อเธรด (สำหรับ debug)
ThreadStateสถานะเธรด (bit flags)
IsAliveเธรดทำงานอยู่หรือไม่
IsBackgroundBackground thread หรือไม่
IsThreadPoolThreadมาจาก ThreadPool หรือไม่
Priorityลำดับความสำคัญ