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.