如何用 C# 顯示當前執行緒的名稱?


使用 Name 屬性顯示 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

更新於:2020-06-20

2K+ 瀏覽

開啟你的 職業生涯

完成課程可獲得認證

開始學習
廣告
© . All rights reserved.