博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#多线程编程实战1.5检测线程状态
阅读量:5018 次
发布时间:2019-06-12

本文共 1017 字,大约阅读时间需要 3 分钟。

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);
}
}
}
}

转载于:https://www.cnblogs.com/tsh292278/p/9238205.html

你可能感兴趣的文章