共用方式為


SemaphoreFullException 類別

定義

對計數已達最大的號誌 (Semaphore) 呼叫 Release 方法時所擲回的例外狀況。

public ref class SemaphoreFullException : Exception
public ref class SemaphoreFullException : SystemException
public class SemaphoreFullException : Exception
public class SemaphoreFullException : SystemException
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class SemaphoreFullException : SystemException
type SemaphoreFullException = class
    inherit Exception
type SemaphoreFullException = class
    inherit SystemException
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type SemaphoreFullException = class
    inherit SystemException
Public Class SemaphoreFullException
Inherits Exception
Public Class SemaphoreFullException
Inherits SystemException
繼承
SemaphoreFullException
繼承
SemaphoreFullException
屬性

範例

下列程式碼範例顯示某個執行緒中的程式設計錯誤如何導致 SemaphoreFullException 另一個執行緒中的 :兩個執行緒進入號志。 第二個執行緒會釋放號志兩次,而第一個執行緒仍在執行其工作。 當第一個執行緒完成並釋放號志時,旗號計數已經滿,而且會擲回例外狀況。

#using <System.dll>
using namespace System;
using namespace System::Threading;

public ref class Example
{
private:
   // A semaphore that can satisfy at most two concurrent
   // requests.
   //
   static Semaphore^ _pool = gcnew Semaphore( 2,2 );

public:
   static void main()
   {
      // Create and start two threads, A and B.
      //
      Thread^ tA = gcnew Thread( gcnew ThreadStart( ThreadA ) );
      tA->Start();

      Thread^ tB = gcnew Thread( gcnew ThreadStart( ThreadB ) );
      tB->Start();
   }

private:
   static void ThreadA()
   {
      // Thread A enters the semaphore and simulates a task
      // that lasts a second.
      //
      _pool->WaitOne();
      Console::WriteLine( L"Thread A entered the semaphore." );

      Thread::Sleep( 1000 );

      try
      {
         _pool->Release();
         Console::WriteLine( L"Thread A released the semaphore." );
      }
      catch ( Exception^ ex ) 
      {
         Console::WriteLine( L"Thread A: {0}", ex->Message );
      }
   }

   static void ThreadB()
   {
      // Thread B simulates a task that lasts half a second,
      // then enters the semaphore.
      //
      Thread::Sleep( 500 );

      _pool->WaitOne();
      Console::WriteLine( L"Thread B entered the semaphore." );
      
      // Due to a programming error, Thread B releases the
      // semaphore twice. To fix the program, delete one line.
      _pool->Release();
      _pool->Release();
      Console::WriteLine( L"Thread B exits successfully." );
   }
};
/* This code example produces the following output:

Thread A entered the semaphore.
Thread B entered the semaphore.
Thread B exits successfully.
Thread A: Adding the given count to the semaphore would cause it to exceed its maximum count.
 */
using System;
using System.Threading;

public class Example
{
    // A semaphore that can satisfy at most two concurrent
    // requests.
    //
    private static Semaphore _pool = new Semaphore(2, 2);

    public static void Main()
    {
        // Create and start two threads, A and B. 
        //
        Thread tA = new Thread(new ThreadStart(ThreadA));
        tA.Start();

        Thread tB = new Thread(new ThreadStart(ThreadB));
        tB.Start();
    }

    private static void ThreadA()
    {
        // Thread A enters the semaphore and simulates a task
        // that lasts a second.
        //
        _pool.WaitOne();
        Console.WriteLine("Thread A entered the semaphore.");

        Thread.Sleep(1000);

        try
        {
            _pool.Release();
            Console.WriteLine("Thread A released the semaphore.");
        }
        catch(Exception ex)
        {
            Console.WriteLine("Thread A: {0}", ex.Message);
        }
    }

    private static void ThreadB()
    {
        // Thread B simulates a task that lasts half a second,
        // then enters the semaphore.
        //
        Thread.Sleep(500);

        _pool.WaitOne();
        Console.WriteLine("Thread B entered the semaphore.");
        
        // Due to a programming error, Thread B releases the
        // semaphore twice. To fix the program, delete one line.
        _pool.Release();
        _pool.Release();
        Console.WriteLine("Thread B exits successfully.");
    }
}
/* This code example produces the following output:

Thread A entered the semaphore.
Thread B entered the semaphore.
Thread B exits successfully.
Thread A: Adding the given count to the semaphore would cause it to exceed its maximum count.
 */
Imports System.Threading

Public Class Example

    ' A semaphore that can satisfy at most two concurrent
    ' requests.
    '
    Private Shared _pool As New Semaphore(2, 2)

    <MTAThread> _
    Public Shared Sub Main()
        ' Create and start two threads, A and B. 
        '
        Dim tA As New Thread(AddressOf ThreadA)
        tA.Start()

        Dim tB As New Thread(AddressOf ThreadB)
        tB.Start()

    End Sub

    Private Shared Sub ThreadA()
        ' Thread A enters the semaphore and simulates a task
        ' that lasts a second.
        '
        _pool.WaitOne()
        Console.WriteLine("Thread A entered the semaphore.")

        Thread.Sleep(1000)

        Try
            _pool.Release()
            Console.WriteLine("Thread A released the semaphore.")
        Catch ex As Exception
            Console.WriteLine("Thread A: {0}", ex.Message)
        End Try
    End Sub

    Private Shared Sub ThreadB()
        ' Thread B simulates a task that lasts half a second,
        ' then enters the semaphore.
        '
        Thread.Sleep(500)

        _pool.WaitOne()
        Console.WriteLine("Thread B entered the semaphore.")
        
        ' Due to a programming error, Thread B releases the
        ' semaphore twice. To fix the program, delete one line.
        _pool.Release()
        _pool.Release()
        Console.WriteLine("Thread B exits successfully.")
    End Sub
End Class
' This code example produces the following output:
'
' Thread A entered the semaphore.
' Thread B entered the semaphore.
' Thread B exits successfully.
' Thread A: Adding the given count to the semaphore would cause it to exceed its maximum count.
'

備註

每次執行緒進入號志時,都會遞減號志上的計數,並線上程釋放旗號時遞增。 當計數為零時,後續要求會封鎖,直到其他執行緒釋放旗號為止。 當所有線程都釋放旗號時,計數會位於建立號志時所指定的最大值。 如果程式設計錯誤造成執行緒此時呼叫 Semaphore.Release 方法, SemaphoreFullException 則會擲回 。

注意

類別 Semaphore 不會對 和 Semaphore.Release 方法的呼叫 WaitHandle.WaitOne 強制執行執行緒識別。 呼叫 的 WaitOne 相同執行緒不需要呼叫 Release

SemaphoreFullException 不一定表示發生例外狀況的程式碼有問題。 請考慮下列案例:執行緒 A 和執行緒 B 輸入最大計數為 2 的號志。 執行緒 B 中的程式設計錯誤會導致呼叫 Release 兩次,讓號志上的計數已滿。 因此,當執行緒 A 最後呼叫 Release 時, SemaphoreFullException 會擲回 。

如需 SemaphoreFullException 類別之執行個體的初始屬性值清單,請參閱 SemaphoreFullException() 建構函式。

建構函式

SemaphoreFullException()

使用預設值,初始化 SemaphoreFullException 類別的新執行個體。

SemaphoreFullException(SerializationInfo, StreamingContext)

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

SemaphoreFullException(String)

使用指定的錯誤訊息,初始化 SemaphoreFullException 類別的新執行個體。

SemaphoreFullException(String, Exception)

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

屬性

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)

適用於

另請參閱