C# 程序检查当前线程的状态

csharpserver side programmingprogramming更新于 2024/9/24 18:41:00

要在 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

相关文章