Để tạo một chuỗi mới.
Thread thread = Thread.CurrentThread; thread.Name = "My new Thread”;
Để nhận id ngữ cảnh hiện tại, hãy sử dụng thuộc tính ContextID.
Thread.CurrentContext.ContextID
Hãy cho chúng tôi xem mã hoàn chỉnh -
Ví dụ
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();
}
}
} Đầu ra
Thread Name = My new Thread current Context id: 0