We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 43,671 times

Contents

Related Categories

.NET Threading Part II - Thread States

randy

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

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

Comments

  • Which Model???

    Posted by belayon on 10 Nov 2004

    Greetings-
    I have been doing alot of reading in this area and am frankly confused on which model/classes to use for a specific implementation I am working on. Perhaps you could shed some light.

    I ...

  • win32 equivalence functions

    Posted by MusicAlly on 12 Feb 2004

    hi, interesting article you've written here. just one question, and that is do you know of a function in .NET/C# equivalent to the old PostThreadMessage. The intention here is not to pass any informat...