Qwertie
Qwertie

Reputation: 17186

What is a suspended thread?

The Thread.Abort() documentation states that it will throw ThreadStateException if:

The thread that is being aborted is currently suspended.

What circumstance is this exactly?

If, for example, the thread is in the middle of a Thread.Sleep(1000), or waiting on a WaitHandle, is it considered "suspended"?

Upvotes: 4

Views: 8253

Answers (3)

Brian Gideon
Brian Gideon

Reputation: 48949

The documentation is rather poor in this regard. Thread.Abort, while not at all advised to use, is much safer in version 2.0 and higher. It will safely unstick most of the .NET BCL blocking calls including Thread.Sleep, WaitHandle.WaitOne, Monitor.Enter, etc. It is my interpretation that the meaning of suspended in this context is a thread that is blocked inside one of these calls. I believe this can be proven with the following code.

static void Main(string[] args)
{
    var thread = new Thread(() => 
        {
            try
            {
                Console.WriteLine("Thread blocking");
                Thread.Sleep(Timeout.Infinite);
                Console.WriteLine("Thread unblocked");
            }
            catch (ThreadAbortException)
            {
                Console.WriteLine("ThreadAbortException");
            }
        });
    Console.WriteLine("Starting thread");
    thread.Start();
    Console.WriteLine("Waiting 1 second");
    Thread.Sleep(1000);
    Console.WriteLine("Aborting thread");
    thread.Abort();
    Console.WriteLine("Waiting for thread to end");
    thread.Join();
    Console.WriteLine("Thread ended");
    Console.ReadLine();
}

Replace the call to Thread.Sleep with the other blocking calls in the BCL to see which ones can be "poked" by Thread.Abort.

Update

My original interpretation is clearly incorrect. The documentation almost explicity tells you what the interpretation should be.

If Abort is called on a thread that has been suspended, a ThreadStateException is thrown in the thread that called Abort, and AbortRequested is added to the ThreadState property of the thread being aborted. A ThreadAbortException is not thrown in the suspended thread until Resume is called.

I will go ahead and leave my answer since it is tangentially relevant to the question.

Upvotes: 1

Jeff Yates
Jeff Yates

Reputation: 62377

From reading the documentation, it is my interpretation that "suspended" in this context does not relate to the use of Thread.Sleep or WaitHandle usage, but instead refers to the thread state encountered when using the obsoleted Thread.Suspend or equivalent Windows API SuspendThread.

In this context, a suspended thread is a thread whose execution has been explicitly paused (or suspended), i.e. it's context is no longer being executed but has been suspended to be resumed at some later point.

This technique is primarily used by debuggers and is not recommended for sychronization activities. This is clarified in the MSDN documentation on SuspendThread:

This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread.

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1500485

A suspended thread is one that has suspended due to a call to Thread.Suspend.

See the ThreadState enum for a list of the states a thread can be in and how it gets there.

Upvotes: 3

Related Questions