Export (0) Print
Expand All

LockRecursionException Class

The exception that is thrown when recursive entry into a lock is not compatible with the recursion policy for the lock.

Namespace:  System.Threading
Assembly:  System.Core (in System.Core.dll)

[SerializableAttribute]
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public class LockRecursionException : Exception

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

LockRecursionException is thrown for several reasons, including the following:

  • If a thread tries to enter an instance of ReaderWriterLockSlim recursively, but the instance does not support recursion.

  • If a thread tries to enter a ReaderWriterLockSlim instance in write mode or upgradeable mode when the thread initially entered the lock in read mode. This represents a potential deadlock and therefore is not allowed.

  • If allowing one more level of recursion would exceed the maximum value for the internal storage counter used to track recursions. This limit is so large that applications should never encounter it.

The following example shows two causes of LockRecursionException when using the ReaderWriterLockSlim class. The program creates a ReaderWriterLockSlim by using the default constructor, which disallows recursion. The program then starts a thread that enters the lock in read mode. The thread tries to enter the lock recursively in read mode, and catches the resulting exception. Finally, the thread tries to enter write mode, which would create a potential for deadlocks. The thread catches the resulting exception.

using System;
using System.Threading;

class Example
{
    // By default, the lock recursion policy for a new  
    // ReaderWriterLockSlim does not allow recursion. 
    static ReaderWriterLockSlim rwls = new ReaderWriterLockSlim();

    static void ThreadProc()
    {
        Console.WriteLine("Acquire the reader lock.");
        rwls.EnterReadLock();

        try
        {
            Console.WriteLine("\nAttempt to acquire the reader lock recursively:");
            rwls.EnterReadLock();
        }
        catch (LockRecursionException lre)
        {
            Console.WriteLine("{0}: {1}", 
                lre.GetType().Name, lre.Message);
        }

        try
        {
            Console.WriteLine("\nAttempt to acquire the writer lock recursively:");
            rwls.EnterWriteLock();
        }
        catch (LockRecursionException lre)
        {
            Console.WriteLine("{0}: {1}", 
                lre.GetType().Name, lre.Message);
        }
    }

    static void Main()
    {
        Thread t = new Thread(ThreadProc);
        t.Start();
        t.Join();
    }
}

/* This code example produces output similar to the following:

Acquire the reader lock.

Attempt to acquire the reader lock recursively:
LockRecursionException: Recursive read lock acquisitions not allowed in this mode.

Attempt to acquire the writer lock recursively:
LockRecursionException: Write lock may not be acquired with read lock held. This pattern is prone to deadlocks. Consider using the upgrade lock.
 */

System.Object
  System.Exception
    System.Threading.LockRecursionException

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Community Additions

ADD
Show:
© 2014 Microsoft