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

วิธีตรวจสอบสถานะเธรดปัจจุบันใน C#

ใช้เมธอด 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