Threading in Delphi for .NET - Threading Priority
(Page 6 of 15 )
System.Threading.ThreadPriority
All manually created threads have an associated priority. This priority allows for boosting or lowering the scheduling of the thread. Table 14.1 lists the enumeration values for ThreadPriority, from the highest to lowest.
Table 14.1 System.Threading.ThreadPriority Enumeration Values
Value | Description |
Highest | Thread is scheduled before any other thread priority. |
AboveNormal | Scheduled before any normal priority threads. |
Normal | Default setting for threads. Scheduled before BelowNormal threads. |
BelowNormal | Scheduled before any threads with lowest priority. |
Lowest | Thread is scheduled after all other higher priority threads. |
A thread's priority is set using its Priority property. Be careful when changing priorities arbitrarily because that can cause hard to debug conditions such as thread starvation, race conditions, and deadlocks. Never use a higher priority when a lower priority will work.
For GUI applications, most background threads should be set to BelowNormal or Lowest to ensure a responsive user interface no matter how much work the background threads are performing.
System.Threading.ThreadState
Once created, a thread has a state that indicates what the thread is doing. Table 14.2 contains the values of the ThreadState enumeration.
Table 14.2 System.Threading.ThreadState Enumeration
Value | Description |
Running | The thread's start method has been called. |
StopRequested | Used internally only, indicates that the thread has been requested to stop executing. |
SuspendRequested | The thread has been requested to suspend itself |
Background | Controlled by the Thread.IsBackground property, this value indicates that the thread is executing in the background. Background threads are automatically aborted when the main thread terminates. Only foreground threads will prevent an application from exiting as long as they are running. |
Unstarted | A thread has been created but not started yet. |
Stopped | The thread is no longer executing. |
WaitSleepJoin | The thread is not running due to either being blocked, sleeping, or joining (waiting for another thread to finish executing). |
Suspended | The thread is suspended. |
AbortRequested | The thread has been requested to abort, but has not received the ThreadAbortException yet. |
Aborted | Indicates that the thread has been terminated because of Thread.Abort. |
Because a thread can be in multiple states, the ThreadState enumeration is a set of bit flags. Not all combinations are valid, however. One valid combination is a thread in a WaitSleepJoin state and an AbortRequested state.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Apartment State and Thread Pooling Class >>
More .NET Articles
More By Xavier Pacheco