C# 程式,檢查當前執行緒狀態


若要檢查 C# 中當前執行緒的狀態,請使用 IsAlive 屬性。

首先,使用 currentThread 屬性顯示執行緒資訊 -

Thread thread = Thread.CurrentThread;

現在使用 thread.IsAlive 屬性檢查執行緒的狀態 -

thread.IsAlive

示例

讓我們看看完成的程式碼,以便在 C# 中檢查當前執行緒的狀態。

線上演示

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 Status = {0}", thread.IsAlive);
         Console.ReadKey();
      }
   }
}

輸出

Thread Status = True

更新於: 19-Jun-2020

3K+ 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.