using System; using System.Collections.Generic; using System.Text; using System.Threading;
namespace ThreadOperation { publicclass Alpha { Int32 i; public Alpha() { i =0; } publicvoid Beta() { while (true) { Console.WriteLine(i.ToString() +":Alpha.Beta is running out of its own thread"); i++; } } }
class Program { staticint Main(string[] args) { Thread.CurrentThread.Name ="System Thread"; Console.WriteLine("Thread Start/Stop/Join sample"); Alpha oAlpha =new Alpha(); Thread oThread =new Thread(new ThreadStart(oAlpha.Beta)); oThread.Start(); oThread.Name ="Alpha Thread"; while (!oThread.IsAlive) Console.WriteLine("has dead"); Console.WriteLine("Current Thread:"+ Thread.CurrentThread.Name); Thread.Sleep(1); Console.WriteLine("oThread.ThreadState:"+oThread.ThreadState); oThread.Abort(); Console.WriteLine("oThread.ThreadState:"+ oThread.ThreadState); oThread.Join(); Console.WriteLine("oThread.ThreadState:"+ oThread.ThreadState); Console.WriteLine(); Console.WriteLine("Alpha.Beta has finished"); try { Console.WriteLine("Try to restart the Alpha.Beta thread"); oThread.Start(); } catch (ThreadStateException) { Console.WriteLine("oThread.ThreadState:"+ oThread.ThreadState); Console.Write("ThreadStateException trying to restart Alpha.Beta. "); Console.WriteLine("Expected since aborted threads cannot be restarted."); Console.ReadLine(); } return0;