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
屬性

範例

以下範例展示了使用該LockRecursionException類別時的ReaderWriterLockSlim兩個原因。 程式透過使用無參數建構子來建立 , 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 實例,而執行緒最初以讀取模式進入鎖。 這代表潛在的僵局,因此不被允許。

  • 如果允許多一層遞迴,會超過用於追蹤遞迴的內部儲存計數器的最大值。 這個限制非常大,應用程式根本不應該遇到。

建構函式

名稱 Description
LockRecursionException()

初始化該類別的新實例 LockRecursionException ,並以系統提供的訊息描述錯誤。

LockRecursionException(SerializationInfo, StreamingContext)
已淘汰.

初始化一個新的類別實例 LockRecursionException ,並使用序列化資料。

LockRecursionException(String, Exception)

初始化類別的新實例 LockRecursionException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。

LockRecursionException(String)

初始化類別的新實例 LockRecursionException ,並以指定訊息描述錯誤。

屬性

名稱 Description
Data

取得一組鍵值對,提供關於例外的額外使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與此例外相關的說明檔案連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,一個編碼的數值,指派給特定例外。

(繼承來源 Exception)
InnerException

會取得 Exception 造成目前例外的實例。

(繼承來源 Exception)
Message

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

(繼承來源 Exception)
Source

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

(繼承來源 Exception)
StackTrace

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

(繼承來源 Exception)
TargetSite

會取得拋出當前例外的方法。

(繼承來源 Exception)

方法

名稱 Description
Equals(Object)

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

(繼承來源 Object)
GetBaseException()

當在派生類別中被覆寫時,回傳 Exception 是一個或多個後續例外的根因。

(繼承來源 Exception)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

在衍生類別中覆寫時,使用例外狀況的相關信息來設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前實例的執行時型態。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

建立並回傳當前例外的字串表示。

(繼承來源 Exception)

事件

名稱 Description
SerializeObjectState
已淘汰.

當例外被序列化以建立包含該例外序列化資料的例外狀態物件時,會發生這種情況。

(繼承來源 Exception)

適用於

另請參閱