using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//检测线程状态
namespace Recipe5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("starting program");
Thread t1 = new Thread(PrintNumbersWithStatus);
Thread t2 = new Thread(DoNothing);
Console.WriteLine(t1.ThreadState.ToString());
t1.Start();
t2.Start();
for (int i = 1; i < 10; i++)
{
Console.WriteLine(t1.ThreadState.ToString());
}
Thread.Sleep(6);
t1.Abort();
Console.WriteLine("a thread has been aborted");
Console.WriteLine(t1.ThreadState.ToString());
Console.WriteLine(t2.ThreadState.ToString());
Console.ReadKey();
}
static void DoNothing()
{
Thread.Sleep(2000);
}
static void PrintNumbersWithStatus()
{
Console.WriteLine("starting...");
Console.WriteLine(Thread.CurrentThread.ThreadState.ToString());
for (int i = 1; i < 10; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine(i);
}
}
}
}