using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace threadtest
{
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(Thread1));
Console.WriteLine("yes,you did well.");
Console.WriteLine(t1.ThreadState);
t1.Start();
Console.WriteLine(t1.ThreadState);
if (t1.ThreadState == ThreadState.Stopped)
t1.Start(); //错误,无法再次启动,除非创建新的线程实例。
}
public static void Thread1()
{
Console.WriteLine("this is a thread test.");
}
}
}
C#中的线程在start之后调用完毕对应的线程处理函数之后就会转变为stopped状态,这个状态是无法再次start的。
那么,如果需要再次start,可以再被调用的线程处理函数中,使线程处于休眠sleep状态,然后根据实际需要再次唤醒即可。
注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。