Working with .NET Threads The .NET class Thread defined in the System.Threading namespace represents a managed thread.The Thread class provides various methods and properties to control the managed thread. Unfortunately, there is a significant potential for abusing these mechanisms, and most developers may not even realize they are doing anything wrong. This article describes the dos and don'ts of the Thread class, and then presents a wrapper class that simplifies starting a thread, correctly terminates a thread, and offers a more consistent class interface than that of the raw Thread class. Managing Thread ID You can get hold of the current thread your code runs on using the CurrentThread read-only static property of the Thread class: public sealed class Thread { public static Thread CurrentThread { get; } // Other methods and properties }
The CurrentThread property returns an instance of the Thread class. Each thread has a unique thread identification number called thread ID. You can access the thread ID via the GetHashCode() method of the Thread class: Thread currentThread = Thread.CurrentThread; int threadID = currentThread.GetHashCode(); Trace.WriteLine("Thread ID is "+ threadID);
Thread.GetHashCode() is guaranteed to return a value that is unique process-wide. It is worth mentioning that the thread ID obtained by GetHashCode() is unrelated to the native thread ID allocated by the underlying operating system. You can verify that by opening the Thread's debug window (under Debug|Windows) during a debug session, and examining the value of the ID column (see Figure 1). The ID column reflects the physical thread ID. Having different IDs allows for .NET threads in the future to map differently to the native operating system support. If you need to programmatically access the physical thread ID your code runs on, use the static method GetCurrentThreadId() of the AppDomain class: int physicalID = AppDomain.GetCurrentThreadId(); int hashedID = Thread.CurrentThread.GetHashCode(); Debug.Assert(physicalID != hashedID);
Another useful property of the Thread class is the Name string property. Name allows you to assign a human-readable name to a thread: Thread currentThread = Thread.CurrentThread; string threadName = "Main UI Thread"; currentThread.Name = threadName;
The thread name can only be assigned by the developer, and by default, a new .NET thread is nameless. Although naming a thread is optional, I highly recommend doing so, because it is an important productivity feature. Windows does not have the ability to assign a name to a thread. In the past, when developers debugged native Windows code, they had to record the new thread ID in every debugging session (using the Threads debug window). These IDs were not only confusing (especially when multiple threads were involved) but also changed in each new debugging session. The Thread's Debug window of Visual Studio .NET (see Figure 1) displays the value of the Name property, thus easing the task of tracing and debugging multithreaded applications. In addition, when a named thread terminates, VS .NET will automatically trace to the Output window the name of that thread, as part of the thread's Exit method.  Figure 1: The Threads Debug window. The name is a good example of a managed-code property that .NET adds to native threads. | & | | 
By: Juval Lowy Juval Löwy is a software architect and the principal of IDesign, a consulting and training company focused on .NET architecture consulting and advanced .NET training. This article contains excerpts from his latest book (Programming .NET Components 2nd Edition (O'Reilly, 2005). Juval is a frequent presenter at development conferences and Microsoft's Regional Director for the Silicon Valley.
Over the last three years Juval has been part of the Strategic Design Review process for .NET 2.0.
Microsoft recognized Juval as a Software Legend as one of the world's top .NET experts and industry leaders.
Contact him at www.idesign.net
| Fast Facts | | The Thread class offers raw control over the underlying operating system thread. However, with power comes liability, and usually most of the Thread class methods and properties should not be used by application developers. The article explains the rational behind its recommendations, and presents a higher-level wrapper class around the Thread type. The wrapper class provides only the safe methods, as well as compensates for basic deficiencies in the Thread class design. | |
|