static void Main() { // Create 6 ThreadedClass objects. ThreadedClass tc1 = new ThreadedClass("Thread1"); ThreadedClass tc2 = new ThreadedClass("Thread2"); ThreadedClass tc3 = new ThreadedClass("Thread3"); ThreadedClass tc4 = new ThreadedClass("Thread4"); ThreadedClass tc5 = new ThreadedClass("Thread5"); ThreadedClass tc6 = new ThreadedClass("Thread6"); Console.WriteLine ("Main - Creating worker threads."); // Create the 6 threads. Thread t1 = new Thread(tc1.ThreadMethod); Thread t2 = new Thread(tc2.ThreadMethod); Thread t3 = new Thread(tc3.ThreadMethod); Thread t4 = new Thread(tc4.ThreadMethod); Thread t5 = new Thread(tc5.ThreadMethod); Thread t6 = new Thread(tc6.ThreadMethod); // Set the priority of each thread. t1.Priority = ThreadPriority.Lowest; t2.Priority = ThreadPriority.Normal; t3.Priority = ThreadPriority.BelowNormal; t4.Priority = ThreadPriority.Highest; t5.Priority = ThreadPriority.AboveNormal; t6.Priority = ThreadPriority.Lowest; Console.WriteLine ("Expect the threads to finish in the following order:"); Console.WriteLine ("\tThread4"); Console.WriteLine ("\tThread5"); Console.WriteLine ("\tThread2"); Console.WriteLine ("\tThread3"); Console.WriteLine ("\tThread1 & Thread6 (order will vary)\n"); // Start the threads in order. Note the order of how // the threads are started. Because of their priority // relative to each other, we can expect #4 to end // first, followed by #5, #2, #3, and #1/#6 since they // have the same priority. t1.Start(); t2.Start(); t3.Start(); t4.Start(); t5.Start(); t6.Start(); Console.WriteLine ("\nMain - Worker threads started.\n"); // Join the last thread that was started. Give the program // two minutes to run before raising an error. if (!t6.Join(120000)) { Console.WriteLine ("\nMain - At least one thread is STILL running."); } // Pause the program. Console.Write ("\nPress <ENTER> to end: "); Console.ReadLine(); }
static void Main() { try { Thread.CurrentThread.Name = "Main Thread"; // Create an instance of the ThreadedClass. ThreadedClass tc1 = new ThreadedClass("Thread1", threadCompleted, false); // Create the thread giving it the delegate with the // method that should be called when the thread starts. Console.WriteLine ("\nMain - Creating thread T1."); Thread t1 = new Thread(tc1.ThreadMethod); t1.Name = "Worker Thread"; // Start the thread and then pause for a moment. t1.Start(); Console.WriteLine("\nMain - T1 thread started."); Thread.Sleep(1000); // Now wait until the thread completes. Console.WriteLine("\nMain - Waiting for signal from T1."); if (TestClass.threadCompleted.WaitOne()) { Console.WriteLine("\nMain - Got signal from T1."); } else { Console.WriteLine("\nMAIN - WaitOne() for T1 timed out!!!!"); } Console.Write("\nPress <ENTER> to continue: "); Console.ReadLine(); Console.Clear(); // Now, we'll test this again, but this time we'll avoid calling Set() // in the thread. See what happens to Main(). tc1 = new ThreadedClass("Thread2", threadCompleted, true); // Create the thread giving it the delegate with the // method that should be called when the thread starts. Console.WriteLine("\nMain - Creating thread T2."); Thread t2 = new Thread(tc1.ThreadMethod); t2.Name = "Worker Thread"; // Start the thread and then pause for a moment. t2.Start(); Console.WriteLine("\nMain - T2 thread started."); Thread.Sleep(1000); // Now wait until the thread completes. Console.WriteLine("\nMain - Waiting for signal from T2."); if (TestClass.threadCompleted.WaitOne(15000, false)) { Console.WriteLine("\nMain - Got signal from T2."); } else { Console.WriteLine("\nMAIN - WaitOne() for T2 timed out!!!!"); } } catch (Exception e) { Console.WriteLine ("\nMAIN - EXCEPTION: {0}", e.Message); } finally { // Pause the program. Usually, this message will appear // before the message from the threaded method appears. Console.Write ("\n *** Main - Press <ENTER> to end: "); Console.ReadLine(); } }
static void Main() { // Get the current thread and set its name. Thread.CurrentThread.Name = "Main Thread"; // Create an instance of the ThreadedClass. ThreadedClass tc1 = new ThreadedClass("Thread1"); // Set the ThreadedClass's boolean internal suspend call to true. tc1.InternalSuspendCall = true; // Create the thread giving it the delegate with the // method that should be called when the thread starts. Console.WriteLine ("Main - Creating thread t1."); Thread t1 = new Thread(tc1.ThreadMethod); t1.Name = "T1 Worker Thread"; // Start the thread t1.Start(); Console.WriteLine ("Main - t1 thread started."); // Sleep for a few seconds. Console.WriteLine("Main - Sleeping for 5 seconds."); Thread.Sleep (5000); // Start another thread ThreadedClass tc2 = new ThreadedClass("Thread2"); tc2.InternalSuspendCall = false; Thread t2 = new Thread(tc2.ThreadMethod); t2.Name = "T2 Worker Thread"; t2.Start(); Console.WriteLine ("Main - t2 thread started."); // Resume the first thread. Console.WriteLine ("Main - Resuming t1 from Suspend() call."); t1.Resume(); // Pause the second thread. Console.WriteLine ("Main - Calling Suspend() on t2."); // *** For some reason, I occasionally see this program hang here when not debugging. t2.Suspend(); // This may throw an exception if t2 is not running. Console.WriteLine ("Main - Thread t2 is now suspended."); // Wait a moment. Console.WriteLine("Main - Sleeping for 5 seconds."); Thread.Sleep(5000); // Resume the second thread. Console.WriteLine ("Main - Resuming t2 from Suspend() call."); t2.Resume(); // Wait a moment. Console.WriteLine("Main - Sleeping for 5 seconds."); Thread.Sleep(5000); // Pause the program. Usually, this message will appear // before the message from the threaded method appears. Console.Write ("Main - Press <ENTER> to end: "); Console.ReadLine(); }
static void Main() { try { Thread.CurrentThread.Name = "Main Thread"; // Create an instance of the ThreadedClass. ThreadedClass tc1 = new ThreadedClass("Thread1"); // Set the ThreadedClass's boolean internal suspend call to true. tc1.InternalSuspendCall = true; // Create the thread giving it the delegate with the // method that should be called when the thread starts. Console.WriteLine ("Main - Creating thread t1."); Thread t1 = new Thread(tc1.ThreadMethod); t1.Name = "Worker Thread"; // Start the thread t1.Start(); Console.WriteLine ("Main - T1 thread started."); // Pause for a few moments. Console.WriteLine ("Main - Sleeping for 5 seconds."); Thread.Sleep(5000); // Abort the thread. //Console.WriteLine("Main - Calling abort on T1."); //t1.Abort(); //// Join the thread. //Console.WriteLine("Main - Joining T1."); //if (!t1.Join(5000)) //{ // Console.WriteLine("\nMain - ERROR: thread did not terminate within allotted time!\n"); //} } catch (Exception e) { Console.WriteLine ("\nMAIN - EXCEPTION: {0}", e.Message); } finally { // Pause the program. Usually, this message will appear // before the message from the threaded method appears. Console.Write ("\n *** Main - Press <ENTER> to end: "); Console.ReadLine(); } }
static void Main() { // First, set the name of the current thread so that it's easier // to view in the debugger. Thread mainThread = Thread.CurrentThread; mainThread.Name = "Main Thread"; // Create an instance of the ThreadedClass. ThreadedClass tc1 = new ThreadedClass("Thread1"); // Set the ThreadedClass's boolean wait forever flag to false. tc1.WaitIndefinitely = false; // Create the thread giving it the delegate with the // method that should be called when the thread starts. Console.WriteLine("Main - Creating first worker thread."); Thread t1 = new Thread(tc1.ThreadMethod); t1.Name = "First Worker Thread"; // Start the thread t1.Start(); Console.WriteLine ("Main - First worker thread started."); // Sleep for a few seconds. Console.WriteLine("Main - Sleeping for 5 seconds."); Thread.Sleep (5000); // Start another thread ThreadedClass tc2 = new ThreadedClass("Thread2"); tc2.WaitIndefinitely = true; Console.WriteLine ("Main - Creating second worker thread."); Thread t2 = new Thread(tc2.ThreadMethod); t2.Name = "Second Worker Thread"; t2.Start(); Console.WriteLine ("Main - Second worker thread started."); // Sleep for a few seconds. Console.WriteLine("Main - Sleeping for 5 seconds."); Thread.Sleep(5000); // Now wake the second worker thread. Console.WriteLine("Main - Interrupting Thread2."); t2.Interrupt(); // Sleep for a few more seconds to give the thread time to finish up. Console.WriteLine("Main - Sleeping for 10 seconds."); Thread.Sleep(10000); Console.Write ("Main - Press <ENTER> to end: "); Console.ReadLine(); }