ใช้ Property Name ของ Thread.CurrentThread
โค้ดตัวอย่าง
using System;
using System.Threading;
namespace Demo
{
class MyClass
{
static void Main(string[] args)
{
Thread thread = Thread.CurrentThread;
thread.Name = "My Thread";
Console.WriteLine("Thread Name = {0}", thread.Name);
Console.WriteLine("Thread ID = {0}", thread.ManagedThreadId);
Console.WriteLine("Is Background = {0}", thread.IsBackground);
Console.WriteLine("Priority = {0}", thread.Priority);
Console.ReadKey();
}
}
}
ผลลัพธ์
Thread Name = My Thread Thread ID = 1 Is Background = False Priority = Normal
คุณสมบัติ Thread สำคัญ
| Property | คำอธิบาย |
|---|---|
| Name | ชื่อ Thread (ตั้งได้/อ่านได้) |
| ManagedThreadId | ID เลขจำนวนเต็ม Unique |
| IsAlive | Thread ทำงานอยู่หรือไม่ |
| IsBackground | Background Thread (ไม่บล็อก Process Exit) |
| Priority | Priority Level (Lowest..Highest) |
| ThreadState | สถานะ (Running, WaitSleepJoin, Stopped, etc.) |
ตั้งชื่อ Thread ใหม่
Thread t = new Thread(DoWork); t.Name = "WorkerThread"; t.Start();