LockRecursionException 類別

定義

當遞迴進入鎖定與鎖定的遞迴原則不相符時,擲回的例外狀況。

public ref class LockRecursionException : Exception
public class LockRecursionException : Exception
[System.Serializable]
public class LockRecursionException : Exception
type LockRecursionException = class
    inherit Exception
[<System.Serializable>]
type LockRecursionException = class
    inherit Exception
Public Class LockRecursionException
Inherits Exception
繼承
LockRecursionException
屬性

範例

下列範例顯示使用 ReaderWriterLockSlim 類別時的兩個 LockRecursionException 原因。 程式會使用無參數建構函式建立 ReaderWriterLockSlim ,不允許遞迴。 然後,程式會啟動執行緒,以讀取模式進入鎖定。 執行緒會嘗試以遞迴方式進入讀取模式的鎖定,並攔截產生的例外狀況。 最後,執行緒會嘗試進入寫入模式,這會建立死結的可能性。 執行緒會攔截產生的例外狀況。

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.
 */
Imports System.Threading

Class Example
    ' By default, the lock recursion policy for a new 
    ' ReaderWriterLockSlim does not allow recursion.
    Private Shared rwls As New ReaderWriterLockSlim()
    
    Shared Sub ThreadProc() 
        Console.WriteLine("Acquire the reader lock.")
        rwls.EnterReadLock()
        
        Try
            Console.WriteLine(vbCrLf & _
                "Attempt to acquire the reader lock recursively:")
            rwls.EnterReadLock()
        Catch lre As LockRecursionException
            Console.WriteLine("{0}: {1}", _
                lre.GetType().Name, lre.Message)
        End Try
        
        Try
            Console.WriteLine(vbCrLf & _
                "Attempt to acquire the writer lock recursively:")
            rwls.EnterWriteLock()
        Catch lre As LockRecursionException
            Console.WriteLine("{0}: {1}", _
                lre.GetType().Name, lre.Message)
        End Try
    
    End Sub 
    
    Shared Sub Main() 

        Dim t As New Thread(AddressOf ThreadProc)
        t.Start()
        t.Join()
    
    End Sub 
End Class 

' 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.
'

備註

LockRecursionException 因為數個原因而擲回,包括下列各項:

  • 如果執行緒嘗試以遞迴方式輸入 實例 ReaderWriterLockSlim ,但實例不支援遞迴。

  • 如果執行緒線上程一開始進入讀取模式時, ReaderWriterLockSlim 嘗試進入寫入模式或可升級模式的實例。 這代表潛在的死結,因此不允許。

  • 如果允許一個以上的遞迴層級超過用來追蹤遞迴的內部儲存計數器最大值。 此限制值相當大,因此應用程式永遠不會發生這個情形。

建構函式

LockRecursionException()

使用系統提供的錯誤描述訊息,初始化 LockRecursionException 類別的新執行個體。

LockRecursionException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 LockRecursionException 類別的新執行個體。

LockRecursionException(String)

初始化具有指定的錯誤描述訊息之 LockRecursionException 類別的新執行個體。

LockRecursionException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 LockRecursionException 類別的新執行個體。

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。

(繼承來源 Exception)
Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已過時。

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱