如何用 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

更新於: 20-Jun-2020

2K+ 檢視次數

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.