Thread类负责在多线程编程中创建和管理线程。它提供了一个称为活着检查线程是否处于活动状态。换句话说, 此属性的值指示线程的当前执行。
语法如下:
public bool IsAlive { get;
}
返回值:该属性返回true如果线程已启动且未正常终止或中止。否则, 返回false。此属性的返回类型为System.Boolean.
以下程序说明了活着属性:
范例1:
// C# program to illustrate the
// use of IsAlive property
using System;
using System.Threading;
public class GFG {// Main Method
static public void Main()
{
Thread thr;
// Get the reference of main Thread
// Using CurrentThread property
thr = Thread.CurrentThread;
// Display the current state of
// the main thread Using IsAlive
// property
Console.WriteLine( "Is main thread is alive" +
" ? : {0}" , thr.IsAlive);
}
}
输出如下:
Is main thread is alive ? : True
范例2:
// C# program to illustrate the
// use of IsAlive property
using System;
using System.Threading;
public class GFG {// Main method
public static void Main()
{
// Creating and initializing threads
Thread Thr1 = new Thread( new ThreadStart(job));
Thread Thr2 = new Thread( new ThreadStart(job));
// Display the current state of
// the threads Using IsAlive
// property
Console.WriteLine( "Is thread 1 is alive : {0}" , Thr1.IsAlive);
Console.WriteLine( "Is thread 2 is alive : {0}" , Thr2.IsAlive);
Thr1.Start();
Thr2.Start();
// Display the current state of
// the threads Using IsAlive
// property
Console.WriteLine( "Is thread 1 is alive : {0}" , Thr1.IsAlive);
Console.WriteLine( "Is thread 2 is alive : {0}" , Thr2.IsAlive);
}// Static method
public static void job()
{
Thread.Sleep(2000);
}
}
输出如下:
Is thread 1 is alive : FalseIs thread 2 is alive : FalseIs thread 1 is alive : TrueIs thread 2 is alive : True
【如何在C#中检查线程是否处于活动状态()】参考:
- https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread.isalive?view=netframework-4.7.2
推荐阅读
- Pandas如何组合Groupby和多个聚合函数()
- Git的安装初次配置以连接github,在第三方软件中配置使用git 命令,提交项目到github的远程仓库出现bug的解决
- win10插入耳机后有电流声怎么办电脑出现回声怎么办
- 网络传输时既有管道流(PipedInputStream 与 PipedOutStream)又有序列化对象反序列化对象(ObjectOutputStream与 ObjectInputStream)
- 安装选择msi格式还是zip(windows下Nodejs zip版下载安装及环境变量配置)
- github项目托管方式(看项目自身是否自带有 .git)
- 安装工具小日记
- 虚拟机VMware 安装centos常规配置共享文件等
- 卸载应用后,删除安装根目录下的所有文件,提示已在另一进程打开,在资源管理器进程中找不到该程序的解决