หากต้องการทราบสถานะของเธรดปัจจุบัน ให้ใช้วิธี IsAlive() -
ขั้นแรก สร้างเธรดใหม่ −
Thread t = Thread.CurrentThread; t.Name = "Our Thread";
ตอนนี้เพื่อรับสถานะของเธรดปัจจุบัน -
t.IsAlive
ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Threading;
namespace Demo {
class MyClass {
static void Main(string[] args) {
Thread t = Thread.CurrentThread;
t.Name = "Our Thread";
Console.WriteLine("Status = {0}", t.IsAlive);
Console.ReadKey();
}
}
} ผลลัพธ์
Status = True