LockRecursionException

System.Threading
Summary: Represents an error that occurs when a thread attempts to acquire a lock that it already holds.

Inheritance

ObjectExceptionSystemExceptionLockRecursionException

Syntax

C#

public sealed class LockRecursionException : SystemException

Constructors

LockRecursionException()

public LockRecursionException()

Initializes a new instance of the LockRecursionException class.

LockRecursionException(string message)

public LockRecursionException(string message)

Initializes a new instance of the LockRecursionException class with a specified error message.

Parameters:

Name Type Description
message string The error message that explains the reason for the exception.

LockRecursionException(string message, Exception innerException)

public LockRecursionException(string message, Exception innerException)

Initializes a new instance of the LockRecursionException class with a specified error message and a reference to the inner exception that is the cause of this exception.

Parameters:

Name Type Description
message string The error message that explains the reason for the exception.
innerException Exception The exception that is the cause of the current exception. If innerException is not null, the current exception is raised in a catch block that handles innerException.

Remarks

This exception is thrown by the Monitor.Enter method when a thread tries to reacquire a lock it already holds. This indicates a potential deadlock situation in multithreaded applications.

To avoid this exception, ensure that locks are properly released and that a thread does not attempt to enter a lock that it already owns. For scenarios requiring reentrant locks, consider using classes like System.Threading.ReaderWriterLockSlim.

Example

C#

using System;
using System.Threading;

public class Example
{
    private static readonly object _lock = new object();

    public static void Main(string[] args)
    {
        Thread thread1 = new Thread(ThreadMethod);
        thread1.Start();

        Thread thread2 = new Thread(ThreadMethod);
        thread2.Start();

        thread1.Join();
        thread2.Join();

        Console.WriteLine("All threads completed.");
    }

    public static void ThreadMethod()
    {
        Console.WriteLine($"{Thread.CurrentThread.Name} attempting to enter lock.");
        lock (_lock)
        {
            Console.WriteLine($"{Thread.CurrentThread.Name} entered lock.");
            Thread.Sleep(100); // Simulate work

            try
            {
                Console.WriteLine($"{Thread.CurrentThread.Name} attempting to re-enter lock.");
                lock (_lock) // This will throw LockRecursionException if the lock is not reentrant
                {
                    Console.WriteLine($"{Thread.CurrentThread.Name} re-entered lock.");
                }
            }
            catch (LockRecursionException ex)
            {
                Console.WriteLine($"Caught LockRecursionException in {Thread.CurrentThread.Name}: {ex.Message}");
            }
            catch (Exception ex)
            {
                 Console.WriteLine($"Caught unexpected exception in {Thread.CurrentThread.Name}: {ex.Message}");
            }
            Console.WriteLine($"{Thread.CurrentThread.Name} exiting lock.");
        }
    }
}