using System; using System.Threading; namespace multithread { public class Alpha
{ public void Beta() { while (true) { Console.WriteLine("Alpha.Beta is running in its own thread."); } }
}; public class Simple { public static int Main() { Console.WriteLine("Thread Start/Stop/Join Sample"); Alpha oAlpha = new Alpha(); //file://这里创建一个线程,使之执行Alpha类的Beta()方法 Thread oThread = new Thread(new ThreadStart(oAlpha.Beta)); oThread.Start(); Console.WriteLine(oThread.ThreadState); Console.WriteLine(oThread.IsAlive);
while (!oThread.IsAlive) Thread.Sleep(1); oThread.Abort(); Console.WriteLine(oThread.IsAlive); oThread.Join(); Console.WriteLine(oThread.IsAlive); Console.WriteLine(); Console.WriteLine("Alpha.Beta has finished"); Console.WriteLine(oThread.ThreadState); try { Console.WriteLine("Try to restart the Alpha.Beta thread"); oThread.Start(); } catch (ThreadStateException) { Console.Write("ThreadStateException trying to restart Alpha.Beta. "); Console.WriteLine("Expected since aborted threads cannot be restarted."); Console.ReadLine(); }
return 0; } } }
我这里运行没问题,第二次也可以运行, 只是最后报异常,你的线程已经Abort不能再开始
你的程序有几点问题, 线程中一直是死循环 而Main中 while (!oThread.IsAlive) Thread.Sleep(1);//最多走到这儿 oThread.Abort();//并不执行
线程一旦在.NET Framework下执行,可能会在程序退出后仍占有一会的资源(也有可能是永不放弃)
所以,在退出之前,一定要记信释放线程.不然,各式各样的系统问题会让你搞不情楚为什么你的机器会那样不稳定.
abort并不能真真解决这个问题.
最好是使用Dispose(),以前我遇到这个问题时用的是GC.SuperFinalize(this);
好久不用了,呵呵.
using System.Threading; namespace multithread
{
¡¡¡¡public class Alpha
¡¡¡¡¡¡¡¡
{
¡¡¡¡¡¡¡¡¡¡¡¡public void Beta()
¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡while (true)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Alpha.Beta is running in its own thread.");
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡
}; ¡¡¡¡¡¡¡¡public class Simple
¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡public static int Main()
¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Thread Start/Stop/Join Sample");¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Alpha oAlpha = new Alpha();
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡//file://ÕâÀï´´½¨Ò»¸öỊ̈߳¬Ê¹Ö®Ö´ÐÐAlphaÀàµÄBeta()·½·¨
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oThread.Start();
Console.WriteLine(oThread.ThreadState);
Console.WriteLine(oThread.IsAlive);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡while (!oThread.IsAlive)
Thread.Sleep(1);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oThread.Abort();
Console.WriteLine(oThread.IsAlive);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oThread.Join();
Console.WriteLine(oThread.IsAlive);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine();
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Alpha.Beta has finished");
Console.WriteLine(oThread.ThreadState);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Try to restart the Alpha.Beta thread");
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oThread.Start();
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡catch (ThreadStateException)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.WriteLine("Expected since aborted threads cannot be restarted.");
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console.ReadLine();
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡return 0;
}
}
}
using System.Threading; namespace multithread
{
public class Alpha
{
public void Beta()
{
while (true)
{
Console.WriteLine("Alpha.Beta is running in its own thread.");
}
}
}; public class Simple
{
public static int Main()
{
Console.WriteLine("Thread Start/Stop/Join Sample"); Alpha oAlpha = new Alpha();
//file://这里创建一个线程,使之执行Alpha类的Beta()方法
Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
oThread.Start();
Console.WriteLine(oThread.ThreadState);
Console.WriteLine(oThread.IsAlive);
while (!oThread.IsAlive)
Thread.Sleep(1);
oThread.Abort();
Console.WriteLine(oThread.IsAlive);
oThread.Join();
Console.WriteLine(oThread.IsAlive);
Console.WriteLine();
Console.WriteLine("Alpha.Beta has finished");
Console.WriteLine(oThread.ThreadState);
try
{
Console.WriteLine("Try to restart the Alpha.Beta thread");
oThread.Start();
}
catch (ThreadStateException)
{
Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
Console.WriteLine("Expected since aborted threads cannot be restarted.");
Console.ReadLine();
}
return 0;
}
}
}
只是最后报异常,你的线程已经Abort不能再开始
线程中一直是死循环
而Main中
while (!oThread.IsAlive)
Thread.Sleep(1);//最多走到这儿
oThread.Abort();//并不执行