home *** CD-ROM | disk | FTP | other *** search
- using System;
- using System.Threading;
-
- public class Alpha
- {
-
- // The method that will be called when the thread is started
- public void Beta()
- {
- while (true)
- {
- Console.WriteLine("Alpha.Beta is running in its own thread.");
- Thread.Sleep(0);
- }
- }
- };
-
- public class Simple
- {
- public static int Main(String[] args)
- {
- Console.WriteLine("Thread Stop Join Sample");
-
- Alpha oAlpha = new Alpha();
-
- // Create the thread object, passing in the Alpha.Beta method
- // via a ThreadStart delegate
- Thread oThread1 = new Thread(new ThreadStart(oAlpha.Beta));
-
- // Start the thread
- oThread1.Start();
-
- // Spin waiting for the started thread to become alive
- while ((oThread1.ThreadState&ThreadState.Unstarted)!=0) ;
-
- // Sleep. Allow oThread1 to do some work.
- Thread.Sleep(100);
-
- // Request that oThread1 be stopped
- oThread1.Stop();
-
- // Wait for the oThread1 to finish
- oThread1.Join();
-
- Console.WriteLine();
- Console.WriteLine("Alpha.Beta has finished");
-
- try
- {
- Console.WriteLine("Try to restart the Alpha.Beta thread");
- oThread1.Start();
- }
- catch (ThreadStateException)
- {
- Console.Write("Cannot restart Alpha.Beta. ");
- Console.WriteLine("Expected since stopped threads cannot be restarted.");
- }
- return 0;
- }
- }