如何在 C# 中查詢執行緒的當前上下文 ID?


建立一個新執行緒。

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

更新於:22-Jun-2020

600 次瀏覽

啟動你的 事業

完成此課程即可獲得認證

開始
廣告
© . All rights reserved.