Determining When a Thread Ends


Often it is useful to know when a thread has ended. In the preceding examples, this was accomplished by watching the count variable—hardly a satisfactory or generalizable solution. Fortunately, Thread provides two means by which you can determine whether a thread has ended. First, you can interrogate the read-only IsAlive property for the thread. It is defined like this:

 public bool IsAlive { get; }

IsAlive returns true if the thread upon which it is called is still running. It returns false otherwise. To try IsAlive, substitute this version of MoreThreads for the one shown in the preceding program:

 // Use IsAlive to wait for threads to end. class MoreThreads {   public static void Main() {     Console.WriteLine("Main thread starting.");     // Construct three threads.     MyThread mt1 = new MyThread("Child #1");     MyThread mt2 = new MyThread("Child #2");     MyThread mt3 = new MyThread("Child #3");     do {       Console.Write(".");       Thread.Sleep(100);     } while (mt1.thrd.IsAlive &&              mt2.thrd.IsAlive &&              mt3.thrd.IsAlive);     Console.WriteLine("Main thread ending.");   } }

This version produces the same output as before. The only difference is that it uses IsAlive to wait for the child threads to terminate.

Another way to wait for a thread to finish is to call Join( ). Its simplest form is shown here:

 public void Join( )

Join( ) waits until the thread on which it is called terminates. Its name comes from the concept of the calling thread waiting until the specified thread joins it. A ThreadStateException will be thrown if the thread has not been started. Additional forms of Join( ) allow you to specify a maximum amount of time that you want to wait for the specified thread to terminate.

Here is a program that uses Join( ) to ensure that the main thread is the last to stop:

 // Use Join(). using System; using System.Threading; class MyThread {   public int count;   public Thread thrd;   public MyThread(string name) {     count = 0;     thrd = new Thread(this.run);     thrd.Name = name;     thrd.Start();   }   // Entry point of thread.   void run() {     Console.WriteLine(thrd.Name + " starting.");     do {       Thread.Sleep(500);       Console.WriteLine("In " + thrd.Name +                         ", count is " + count);       count++;     } while(count < 10);     Console.WriteLine(thrd.Name + " terminating.");   } } // Use Join() to wait for threads to end. class JoinThreads {   public static void Main() {     Console.WriteLine("Main thread starting.");     // Construct three threads.     MyThread mt1 = new MyThread("Child #1");     MyThread mt2 = new MyThread("Child #2");     MyThread mt3 = new MyThread("Child #3");     mt1.thrd.Join();     Console.WriteLine("Child #1 joined.");     mt2.thrd.Join();     Console.WriteLine("Child #2 joined.");     mt3.thrd.Join();     Console.WriteLine("Child #3 joined.");     Console.WriteLine("Main thread ending.");   } }

Sample output from this program is shown here. Remember that, when you try the program, your output may vary slightly.

 Main thread starting. Child #1 starting. Child #2 starting. Child #3 starting. In Child #1, count is 0 In Child #2, count is 0 In Child #3, count is 0 In Child #1, count is 1 In Child #2, count is 1 In Child #3, count is 1 In Child #1, count is 2 In Child #2, count is 2 In Child #3, count is 2 In Child #1, count is 3 In Child #2, count is 3 In Child #3, count is 3 In Child #1, count is 4 In Child #2, count is 4 In Child #3, count is 4 In Child #1, count is 5 In Child #2, count is 5 In Child #3, count is 5 In Child #1, count is 6 In Child #2, count is 6 In Child #3, count is 6 In Child #1, count is 7 In Child #2, count is 7 In Child #3, count is 7 In Child #1, count is 8 In Child #2, count is 8 In Child #3, count is 8 In Child #1, count is 9 Child #1 terminating. In Child #2, count is 9 Child #2 terminating. In Child #3, count is 9 Child #3 terminating. Child #1 joined. Child #2 joined. Child #3 joined. Main thread ending.

As you can see, after the calls to Join( ) return, the threads have stopped executing.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net