The .NET Framework thread model is designed to model an execution thread. Many of the Threading namespace classes and members map directly to an execution state of a thread. Personally, I found knowing the execution states of a thread ultimately made it easier for me to understand threading, so using Figure 16-1 and Table 16-1, I'll walk you through the state and the action required to change states within the .NET Framework thread model.
ACTION | STATE |
---|---|
The thread is created with the CLR and has not been invoked. | Unstarted |
The thread executes its start process. | Running |
The thread continues to run until another action occurs. | Running |
The running thread calls sleep for a specified length of time. | WaitSleepJoin |
The running thread calls wait on a locked resource. | WaitSleepJoin |
The running thread calls join on another thread. | WaitSleepJoin |
Another thread calls interrupt on the WaitSleepJoin thread. | Running |
Another thread calls suspend on the thread. | SuspendRequest |
The SuspendRequested thread processes the suspend call. | Suspended |
Another thread calls resume on a Suspended thread. | Running |
Another thread calls abort on the thread. | AbortRequest |
The AbortRequested thread processes the abort call. | Aborted |
Figure 16-1: The execution states of a thread
You might want to note that the states in Table 16-1 map directly to the System::Threading::ThreadState enumeration. And, if you need to determine the current state, you would look in the ThreadState property in the Thread class.
In addition to these states is a Background state, which means the thread is executing in the background (as opposed to in the foreground). The biggest difference between a background thread and a foreground thread is that a background thread ends when the main application thread ends. A foreground thread continues executing until it is aborted or finishes executing. You set a thread to be in the background by setting the IsBackground property of the Thread class.