Quiz #10 (the last one)!

1. Write a complete C# program that creates 2 threads (refer to them as T1 and T2).

The code for thread T1 continually checks to see if thread T2 has been created (i.e., if T2 != null). If T2 has been created, T1 suspends thread T2 and writes the message "T1 wins" on the Console window.

The code for Thread T2 continually checks to see if thread T1 has been created. If T1 has been created, thread T2 suspends thread T1 and writes the message "T2 wins" on the Console window.

You will need to make the Thread variables T1 and T2 global. The simplest solution will also define separate methods for the two threads to run.

Don't forget to include System.Threading;

using System;
using System.Threading;

namespace ConsoleApplication1
{
   class ThreadRace
   {
      Thread t1, t2;
		
      public ThreadRace() 
      {
         t1 = new Thread(new ThreadStart(Run1));
         t1.Start();
         t2 = new Thread(new ThreadStart(Run2));
         t2.Start();
      }
		
      public void Run1()
      {
         while (t2 == null); 
         t2.Suspend();
         Console.WriteLine("T1 wins");
      }

      public void Run2()
      {
         while (t1 == null);
         t1.Suspend();
         Console.WriteLine("T2 wins");
      }

      static void Main(string[] args)
      {
         ThreadRace threadRace = new ThreadRace();
      }
   }
}