เพื่อสร้างกระทู้ใหม่
Thread thread = Thread.CurrentThread; thread.Name = "My new Thread”;
หากต้องการรับ ID บริบทปัจจุบัน ให้ใช้คุณสมบัติ ContextID
Thread.CurrentContext.ContextID
ให้เราดูรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Threading;
namespace Demo {
class MyClass {
static void Main(string[] args) {
Thread thread = Thread.CurrentThread;
thread.Name = "My new Thread";
Console.WriteLine("Thread Name = {0}", thread.Name);
Console.WriteLine("current Context id: {0}", Thread.CurrentContext.ContextID);
Console.ReadKey();
}
}
} ผลลัพธ์
Thread Name = My new Thread current Context id: 0