如何獲取 C# 中當前執行緒的狀態?


要獲取當前執行緒的狀態,使用 IsAlive() 方法 -

首先,建立一個新執行緒 -

Thread t = Thread.CurrentThread;
t.Name = "Our Thread";

現在,要獲取當前執行緒的狀態 -

t.IsAlive

以下為完整程式碼 -

示例

 即時演示

using System;
using System.Threading;

namespace Demo {
   class MyClass {
      static void Main(string[] args) {
         Thread t = Thread.CurrentThread;
         t.Name = "Our Thread";

         Console.WriteLine("Status = {0}", t.IsAlive);
         Console.ReadKey();
      }
   }
}

輸出

Status = True

更新於: 22-Jun-2020

183 次觀看

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.