using System;
using System.Threading;

namespace ClassExamples {
   class A {
      public void DoWork() {
         try {
            while (true) {
               Console.WriteLine("A is doing work in its own thread");
               Thread.Sleep(500);
            }
         }
         catch (ThreadAbortException) {
            Console.WriteLine("\n****Has been asked to abort");
         }
      }
   }

   class Tester {
      public static void Main() {
         Console.WriteLine("Thread Start/Stop/Join Example");

         A a = new A();
         Thread t = new Thread(new ThreadStart(a.DoWork));   // notice the delegate ThreadStart

         t.Start();

         // this is not necessary .. just here to demonstrate the "IsAlive" property
         while(t.IsAlive == false);    // spin until worker thread has started

         Thread.Sleep(5000);  // let the Main() thread sleep for 1 millisecond

         t.Abort();  // send Abort signal to thread t

         t.Join();   // halt Main thread until thread t finishes

         Console.WriteLine("----\nThe \"DoWork\" thread has finished");

         try {
            Console.WriteLine("Trying to restart the worker thread again");
            t.Start();
         } catch (ThreadStateException) {
            Console.WriteLine("This exception is expected because aborted threads can not be restarted");
         }
      }
   }
}