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

โปรแกรม C# แสดง Priority ของ Thread

ใช้ property Priority ของคลาส Thread เพื่อดู/ตั้งค่าระดับความสำคัญของเธรด

ค่า Priority ที่เป็นไปได้ (enum ThreadPriority):

  • Lowest
  • BelowNormal
  • Normal (ค่าเริ่มต้น)
  • AboveNormal
  • Highest

ตัวอย่างโค้ดเต็ม

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 Priority = {0}", thread.Priority);
            Console.ReadKey();
        }
    }
}

ผลลัพธ์

Thread Priority = Normal

คำอธิบาย: Thread.CurrentThread ให้ออบเจ็กต์เธรดที่กำลังทำงานอยู่ สามารถตั้งค่า Priority ได้ด้วย thread.Priority = ThreadPriority.Highest;