C# 程式顯示當前執行緒名稱


若要顯示 C# 中當前執行緒的名稱,請使用 Name 屬性。

首先,使用 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

更新於: 19-Jun-2020

183 瀏覽次數

開啟您的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.