Condividi tramite


DangerousThreadingAPI 

The DangerousThreadingAPI managed debugging assistant (MDA) is activated when the System.Threading.Thread.Suspend method is called on a thread other than the current thread.

Symptoms

An application is unresponsive or hangs indefinitely. System or application data might be left in an unpredictable state temporarily or even after an application has been shut down. Some operations are not completing as expected.

Symptoms can vary widely due to the randomness inherent to the problem.

Cause

A thread is asynchronously suspended by another thread using the Suspend method. There is no way to determine when it is safe to suspend another thread which might be in the middle of an operation. Suspending the thread can result in the corruption of data or the breaking of invariants. Should a thread be placed into a suspended state and never resumed using the Resume method, the application can hang indefinitely and possibly damage application data. These methods have been marked as obsolete.

If synchronization primitives are held by the target thread, they remain held during suspension. This can lead to deadlocks should another thread, for example the thread performing the Suspend, attempt to acquire a lock on the primitive. In this situation, the problem manifests itself as a deadlock.

Resolution

Avoid designs that require the use of Suspend and Resume. For cooperation between threads, use synchronization primitives such as Monitor, ReaderWriterLock, Mutex, or the C# lockstatement. If you must use these methods, reduce the window of time and minimize the amount of code that executes while the thread is in a suspended state.

Effect on the Runtime

This MDA has no effect on the CLR. It only reports data about dangerous threading operations.

Output

The MDA identifies the dangerous threading method that caused it to be activated.

Configuration

<mdaConfig>
  <assistants>
    <dangerousThreadingAPI />
  </assistants>
</mdaConfig>

Example

The following code example demonstrates a call to the Suspend method that causes the activation of the DangerousThreadingAPI.

using System.Threading;
void FireMda()
{
    Thread t = new Thread(delegate() { Thread.Sleep(1000); });
    t.Start();
    // The following line activates the MDA.
    t.Suspend(); 
    t.Resume();
    t.Join();
}

See Also

Reference

lock Statement (C# Programmers Reference)
Thread

Concepts

Diagnosing Errors with Managed Debugging Assistants