Free mag vol1 | Page 767

CHAPTER 19  MULTITHREADED, PARALLEL, AND ASYNC PROGRAMMING Join() Blocks the calling thread until the specified thread (the one on which Join() is called) exits. Resume() Resumes a thread that has been previously suspended. Start() Instructs the CLR to execute the thread ASAP. Suspend() Suspends the thread. If the thread is already suspended, a call to Suspend() has no effect.  Note Aborting or suspending an active thread is generally considered a bad idea. When you do so, there is a chance (however small) that a thread could “leak” its workload when disturbed or terminated. Obtaining Statistics About the Current Thread of Execution Recall that the entry point of an executable assembly (i.e., the Main() method) runs on the primary thread of execution. To illustrate the basic use of the Thread type, assume you have a new Console Application named ThreadStats. As you know, the static Thread.CurrentThread property retrieves a Thread object that represents the currently executing thread. Once you have obtained the current thread, you are able to print out various statistics, like so: // Be sure to import the System.Threading namespace. static void Main(string[] args) { Console.WriteLine("***** Primary Thread stats *****\n"); // Obtain and name the current thread. Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "ThePrimaryThread"; // Show details of hosting AppDomain/Context. Console.WriteLine("Name of current AppDomain: {0}", Thread.GetDomain().FriendlyName); Console.WriteLine("ID of current Context: {0}", Thread.CurrentContext.ContextID); // Print out some stats about this thread. Console.WriteLine("Thread Name: {0}", primaryThread.Name); Console.WriteLine("Has thread started?: {0}", primaryThread.IsAlive); Console.WriteLine("Priority Level: {0}", primaryThread.Priority); Console.WriteLine("Thread State: {0}", primaryThread.ThreadState); Console.ReadLine(); 712