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

C# โปรแกรมแสดงชื่อกระทู้ปัจจุบัน


หากต้องการแสดงชื่อของเธรดปัจจุบันใน C# ให้ใช้ ชื่อ ทรัพย์สิน

ประการแรก ใช้ currentThread คุณสมบัติเพื่อแสดงข้อมูลเกี่ยวกับเธรด -

Thread thread = Thread.CurrentThread;

ตอนนี้ใช้ thread.Name คุณสมบัติเพื่อแสดงชื่อเธรด -

thread.Name

ตัวอย่าง

ให้เราดูโค้ดแบบเต็มแสดงชื่อเธรดปัจจุบันในภาษา C#

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