ใช้ property Name ของคลาส Thread เพื่อตั้ง/อ่านชื่อเธรด
ตัวอย่างโค้ดเต็ม
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.ReadKey();
}
}
}
ผลลัพธ์
Thread Name = My Thread
คำอธิบาย: ตั้งชื่อเธรดช่วยให้ Debugging ง่ายขึ้น เพราะใน Visual Studio Threads window จะแสดงชื่อแทน Thread ID ตัวเลข