Library tutorials & articles

.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

Comments

  1. 10 Nov 2004 at 21:30
    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 have a large treeview on a UI thread, with nodes that contain collections of images that take a long time to render previews for in a panel. I would like to let a worker thread render the set of previews based on the treeview node selected, passing each preview back to the ui thread as they complete. Of course, it's bad form to have a worker thread access objects in the UI thread.

    I have tried async delegate calls and got it working on the UI thread, but it still locks the UI while it's rendering the previews. I am confused about wether to use events, a ThreadPool, Mutex stuff, or what?

    Can you help me out here?

    Many Thanks
  2. 12 Feb 2004 at 11:57

    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 information to a spawned thread via the LPARAM parameter, but rather just to inform the thread of the fact that some information is ready for them to collect (available at a singleton object which is made thread-safe). I tried using the approach of firing events and having the event handled by a delegate but only managed to make this work in the context of the same thread, not across threads. Any ideas?


    Thanks


    Alastair.

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of .NET Threading Part II.

Leave a comment

Sign in or Join us (it's free).

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

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Want to stay in touch with what's going on? Follow us on twitter!