GOTCHA #48 The Thread classThread classThe THRead class provides a number of methods and properties to work with a specific thread. There are certain members in the Thread class, however, that you should not use, or use only with extreme caution. Here are some you should think twice about:
Now let's look at some potentially dangerous methods:
If you think you need any of the methods mentioned above, you should revisit your design (For good discussions of multithreading issues, refer to [Lea00].) The most effective way to communicate between threads is to use what I call bait. Set some fields to a certain (boolean) value and let the other thread check for that value to take a particular action or terminate. You might also consider other facilities, such as Wait(), Pulse(), Join(), and synchronizing on a wait handle. IN A NUTSHELLCertain methods and properties of the THRead class have unpredictable behavior. Avoid them, or use them with extreme caution. SEE ALSOGotcha #49, "Foreground threads may prevent a program from terminating," Gotcha #53, "Environment.Exit() brings down the CLR," and Gotcha #54, "ResetAbort() may lead to surprises." |