.NET Threading Part II

Thread States

The last few topics in this article are really just the few bits of reference information I dug up on .NET threads. This section describes the states of a thread. The Thread object in the .NET framework has a property called the ThreadState, which is one of the members of the following enumeration, which I pulled from the .NET documentation.

public enum ThreadState {
    Running = 0,
    SuspendRequested = 2,
    Background = 4,
    Unstarted = 8,
    WaitSleepJoin = 32,
    Suspended = 64,
    AbortRequested = 128,
    Aborted = 256
};

Unfortunately, I have been able to generate ThreadState's that are not in this enumeration. Specifically, the Stopped ThreadState seems to be missing and is easy to generate. If you check the state of a thread that has run to completion, then the state is marked as Stopped. What I also found is that it is quite easy to generate dual states. You can be in the AbortRequested state and the WaitSleepJoin state. If you catch the

ThreadAbortException and then call Thread. Sleep, then the ThreadState will be "WaitSleepJoin, AbortRequested", a dual state. The same is true if you are sleeping when the Suspend instance method is called. Immediately after the call to the Suspend instance method, the ThreadState property reports "SuspendRequested, WaitSleepJoin", then quickly changes to "WaitSleepJoin, Suspended". I've encountered a few state diagrams that tried to depict the state transitions

of .NET threads. I must say that most are misleading or incomplete. The biggest problem is that most of the state diagrams did not attempt to account for dual states. My own attempt at the state diagram, I know, is still lacking but much further along then anything else I've seen (see Figure 1).

Figure 1: State Diagram 7

You might also like...

Comments

About the author

Randy Charles Morin

Randy Charles Morin Canada

Randy's article are Copyright 1998-2003 Randy Charles Morin

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”