C# 判断线程是否结束Alive

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace _5006_判断线程是否结束Alive
{
    class UsingIsAlive
    {
        static void Main()//IsAlive 是否是活动线程,不是死线程
        {
            UsingIsAlive myUsingIsAlive = new UsingIsAlive();
            Thread myThread = new Thread
            (new ThreadStart(myUsingIsAlive.justSleep));
            myThread.Start();//执行justSleep()
            do
            {
                Thread.Sleep(1000);//让主线程停止1秒再执行
                Console.WriteLine
                       ("myThread 线程目前状态: {0} ",
                    myThread.IsAlive.ToString());
                Console.WriteLine("...........");

            } while (myThread.IsAlive);//线程存活的时候循环执行,否则退出当前,执行下面的代码
            Console.WriteLine
                       ("开始阻止当前线程 ");
            myThread.Join();//暂停当前线程,执行justSleep(),但发现justSleep()已经结束
            Console.WriteLine
                ("myThread 线程结束 !!:{0} ", myThread.IsAlive);
            Console.ReadLine();
            //justSleep()暂停n秒,暂停的过程中判断其所在线程的状态

        }
        void justSleep()
        {
            Console.WriteLine("开始启动线程的执行!!");
            Console.WriteLine("暂停线程myThread 执行10秒!!");
            Console.WriteLine("========================================");
            Thread.Sleep(10000);//停止10秒
            //int i = 0;
        }
    }
}

 

你可能感兴趣的:(C#,后端)