AbandonedMutexException クラス

定義

スレッドが、別のスレッドが解放せずに終了することによって放棄した Mutex オブジェクトを取得したときにスローされる例外。The exception that is thrown when one thread acquires a Mutex object that another thread has abandoned by exiting without releasing it.

public ref class AbandonedMutexException : Exception
public ref class AbandonedMutexException : SystemException
public class AbandonedMutexException : Exception
public class AbandonedMutexException : SystemException
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class AbandonedMutexException : SystemException
type AbandonedMutexException = class
    inherit Exception
type AbandonedMutexException = class
    inherit SystemException
Public Class AbandonedMutexException
Inherits Exception
Public Class AbandonedMutexException
Inherits SystemException
継承
AbandonedMutexException
継承
AbandonedMutexException
属性

次のコード例では、5つの mutex を破棄し、WaitOneWaitAny、および WaitAll メソッドに対する影響を示すスレッドを実行します。The following code example executes a thread that abandons five mutexes, demonstrating their effects on the WaitOne, WaitAny, and WaitAll methods. WaitAny 呼び出しの MutexIndex プロパティの値が表示されます。The value of the MutexIndex property is displayed for the WaitAny call.

注意

WaitAny メソッドの呼び出しが、破棄されたミューテックスのいずれかによって中断されています。The call to the WaitAny method is interrupted by one of the abandoned mutexes. その他の破棄されたミューテックスでも、後続の待機メソッドによって AbandonedMutexException がスローされる可能性があります。The other abandoned mutex could still cause an AbandonedMutexException to be thrown by subsequent wait methods.

using namespace System;
using namespace System::Threading;

namespace SystemThreadingExample
{
    public ref class Example
    {
    private:
        static ManualResetEvent^ dummyEvent = 
            gcnew ManualResetEvent(false);
            
        static Mutex^ orphanMutex1 = gcnew Mutex;
        static Mutex^ orphanMutex2 = gcnew Mutex;
        static Mutex^ orphanMutex3 = gcnew Mutex;
        static Mutex^ orphanMutex4 = gcnew Mutex;
        static Mutex^ orphanMutex5 = gcnew Mutex;
        
    public:
        static void ProduceAbandonMutexException(void)
        {
            
            // Start a thread that grabs all five mutexes, and then
            // abandons them.
            Thread^ abandonThread = 
                gcnew Thread(gcnew ThreadStart(AbandonMutex));

            abandonThread->Start();
            
            // Make sure the thread is finished.
            abandonThread->Join();
            
            // Wait on one of the abandoned mutexes. The WaitOne
            // throws an AbandonedMutexException.
            try
            {
                orphanMutex1->WaitOne();
                Console::WriteLine("WaitOne succeeded.");
            }
            catch (AbandonedMutexException^ ex) 
            {
                Console::WriteLine("Exception in WaitOne: {0}", 
                    ex->Message);
            }
            finally
            {
                
                // Whether or not the exception was thrown, 
                // the current thread owns the mutex, and 
                // must release it.
                orphanMutex1->ReleaseMutex();
            }

            
            // Create an array of wait handles, consisting of one
            // ManualResetEvent and two mutexes, using two more of
            // the abandoned mutexes.
            array <WaitHandle^>^ waitFor = {dummyEvent, 
                orphanMutex2, orphanMutex3};
            
            // WaitAny returns when any of the wait handles in the 
            // array is signaled. Either of the two abandoned mutexes
            // satisfy the wait, but lower of the two index values is
            // returned by MutexIndex. Note that the Try block and
            // the Catch block obtain the index in different ways.
            try
            {
                int index = WaitHandle::WaitAny(waitFor);
                Console::WriteLine("WaitAny succeeded.");
                (safe_cast<Mutex^>(waitFor[index]))->ReleaseMutex();
            }
            catch (AbandonedMutexException^ ex) 
            {
                Console::WriteLine("Exception in WaitAny at index {0}"
                    "\r\n\tMessage: {1}", ex->MutexIndex, 
                    ex->Message);
                (safe_cast<Mutex^>(waitFor[ex->MutexIndex]))->
                    ReleaseMutex();
            }

            orphanMutex3->ReleaseMutex();
            
            // Use two more of the abandoned mutexes for the WaitAll 
            // call. WaitAll doesn't return until all wait handles 
            // are signaled, so the ManualResetEvent must be signaled 
            // by calling Set().
            dummyEvent->Set();
            waitFor[1] = orphanMutex4;
            waitFor[2] = orphanMutex5;
            
            // Because WaitAll requires all the wait handles to be
            // signaled, both mutexes must be released even if the
            // exception is thrown. Thus, the ReleaseMutex calls are 
            // placed in the Finally block. Again, MutexIndex returns
            // the lower of the two index values for the abandoned
            // mutexes.
            //  
            try
            {
                WaitHandle::WaitAll(waitFor);
                Console::WriteLine("WaitAll succeeded.");
            }
            catch (AbandonedMutexException^ ex) 
            {
                Console::WriteLine("Exception in WaitAny at index {0}"
                    "\r\n\tMessage: {1}", ex->MutexIndex, 
                    ex->Message);
            }
            finally
            {
                orphanMutex4->ReleaseMutex();
                orphanMutex5->ReleaseMutex();
            }

        }


    private:
        [MTAThread]
        static void AbandonMutex()
        {
            orphanMutex1->WaitOne();
            orphanMutex2->WaitOne();
            orphanMutex3->WaitOne();
            orphanMutex4->WaitOne();
            orphanMutex5->WaitOne();
            Console::WriteLine(
                "Thread exits without releasing the mutexes.");
        }
    };   
}

//Entry point of example application
[MTAThread]
int main(void)
{
    SystemThreadingExample::Example::ProduceAbandonMutexException();
}

// This code example produces the following output:
// Thread exits without releasing the mutexes.
// Exception in WaitOne: The wait completed due to an abandoned mutex.
// Exception in WaitAny at index 1
//         Message: The wait completed due to an abandoned mutex.
// Exception in WaitAll at index -1
//         Message: The wait completed due to an abandoned mutex.


using System;
using System.Threading;

public class Example
{
    private static ManualResetEvent _dummy = new ManualResetEvent(false);

    private static Mutex _orphan1 = new Mutex();
    private static Mutex _orphan2 = new Mutex();
    private static Mutex _orphan3 = new Mutex();
    private static Mutex _orphan4 = new Mutex();
    private static Mutex _orphan5 = new Mutex();

    [MTAThread]
    public static void Main()
    {
        // Start a thread that takes all five mutexes, and then
        // ends without releasing them.
        //
        Thread t = new Thread(new ThreadStart(AbandonMutex));
        t.Start();
        // Make sure the thread is finished.
        t.Join();

        // Wait on one of the abandoned mutexes. The WaitOne returns
        // immediately, because its wait condition is satisfied by
        // the abandoned mutex, but on return it throws
        // AbandonedMutexException.
        try
        {
            _orphan1.WaitOne();
            Console.WriteLine("WaitOne succeeded.");
        }
        catch(AbandonedMutexException ex)
        {
            Console.WriteLine("Exception on return from WaitOne." +
                "\r\n\tMessage: {0}", ex.Message);
        }
        finally
        {
            // Whether or not the exception was thrown, the current
            // thread owns the mutex, and must release it.
            //
            _orphan1.ReleaseMutex();
        }

        // Create an array of wait handles, consisting of one
        // ManualResetEvent and two mutexes, using two more of the
        // abandoned mutexes.
        WaitHandle[] waitFor = {_dummy, _orphan2, _orphan3};

        // WaitAny returns when any of the wait handles in the 
        // array is signaled, so either of the two abandoned mutexes
        // satisfy its wait condition. On returning from the wait,
        // WaitAny throws AbandonedMutexException. The MutexIndex
        // property returns the lower of the two index values for 
        // the abandoned mutexes. Note that the Try block and the
        // Catch block obtain the index in different ways.
        //  
        try
        {
            int index = WaitHandle.WaitAny(waitFor);
            Console.WriteLine("WaitAny succeeded.");

            // The current thread owns the mutex, and must release
            // it.
            Mutex m = waitFor[index] as Mutex;
            if (m != null) m.ReleaseMutex();
        }
        catch(AbandonedMutexException ex)
        {
            Console.WriteLine("Exception on return from WaitAny at index {0}." +
                "\r\n\tMessage: {1}", ex.MutexIndex, ex.Message);

            // Whether or not the exception was thrown, the current
            // thread owns the mutex, and must release it.
            //
            if (ex.Mutex != null) ex.Mutex.ReleaseMutex();
        }

        // Use two more of the abandoned mutexes for the WaitAll call.
        // WaitAll doesn't return until all wait handles are signaled,
        // so the ManualResetEvent must be signaled by calling Set().
        _dummy.Set();
        waitFor[1] = _orphan4;
        waitFor[2] = _orphan5;

        // The signaled event and the two abandoned mutexes satisfy
        // the wait condition for WaitAll, but on return it throws
        // AbandonedMutexException. For WaitAll, the MutexIndex
        // property is always -1 and the Mutex property is always
        // null.
        //  
        try
        {
            WaitHandle.WaitAll(waitFor);
            Console.WriteLine("WaitAll succeeded.");
        }
        catch(AbandonedMutexException ex)
        {
            Console.WriteLine("Exception on return from WaitAll. MutexIndex = {0}." +
                "\r\n\tMessage: {1}", ex.MutexIndex, ex.Message);
        }
        finally
        {
            // Whether or not the exception was thrown, the current
            // thread owns the mutexes, and must release them.
            //
            _orphan4.ReleaseMutex();
            _orphan5.ReleaseMutex();
        }
    }

    [MTAThread]
    public static void AbandonMutex()
    {
        _orphan1.WaitOne();
        _orphan2.WaitOne();
        _orphan3.WaitOne();
        _orphan4.WaitOne();
        _orphan5.WaitOne();
        // Abandon the mutexes by exiting without releasing them.
        Console.WriteLine("Thread exits without releasing the mutexes.");
    }
}

/* This code example produces the following output:

Thread exits without releasing the mutexes.
Exception on return from WaitOne.
        Message: The wait completed due to an abandoned mutex.
Exception on return from WaitAny at index 1.
        Message: The wait completed due to an abandoned mutex.
Exception on return from WaitAll. MutexIndex = -1.
        Message: The wait completed due to an abandoned mutex.
 */
Option Explicit
Imports System.Threading

Public Class Example
    Private Shared _dummy As New ManualResetEvent(False)

    Private Shared _orphan1 As New Mutex()
    Private Shared _orphan2 As New Mutex()
    Private Shared _orphan3 As New Mutex()
    Private Shared _orphan4 As New Mutex()
    Private Shared _orphan5 As New Mutex()

    <MTAThread> _
    Public Shared Sub Main()
        ' Start a thread that takes all five mutexes, and then
        ' ends without releasing them.
        '
        Dim t As New Thread(AddressOf AbandonMutex)
        t.Start()
        ' Make sure the thread is finished.
        t.Join()

        ' Wait on one of the abandoned mutexes. The WaitOne returns
        ' immediately, because its wait condition is satisfied by
        ' the abandoned mutex, but on return it throws
        ' AbandonedMutexException.
        Try
            _orphan1.WaitOne()
            Console.WriteLine("WaitOne succeeded.")
        Catch ex As AbandonedMutexException
            Console.WriteLine("Exception on return from WaitOne." _
                & vbCrLf & vbTab & "Message: " _
                & ex.Message) 
        Finally
            ' Whether or not the exception was thrown, the current
            ' thread owns the mutex, and must release it.
            '
            _orphan1.ReleaseMutex()
        End Try

        ' Create an array of wait handles, consisting of one
        ' ManualResetEvent and two mutexes, using two more of the
        ' abandoned mutexes.
        Dim waitFor(2) As WaitHandle
        waitFor(0) = _dummy
        waitFor(1) = _orphan2
        waitFor(2) = _orphan3

        ' WaitAny returns when any of the wait handles in the 
        ' array is signaled, so either of the two abandoned mutexes
        ' satisfy its wait condition. On returning from the wait,
        ' WaitAny throws AbandonedMutexException. The MutexIndex
        ' property returns the lower of the two index values for 
        ' the abandoned mutexes. Note that the Try block and the
        ' Catch block obtain the index in different ways.
        '  
        Try
            Dim index As Integer = WaitHandle.WaitAny(waitFor)
            Console.WriteLine("WaitAny succeeded.")

            Dim m As Mutex = TryCast(waitFor(index), Mutex)

            ' The current thread owns the mutex, and must release
            ' it.
            If m IsNot Nothing Then m.ReleaseMutex()
        Catch ex As AbandonedMutexException
            Console.WriteLine("Exception on return from WaitAny at index " _
                & ex.MutexIndex & "." _
                & vbCrLf & vbTab & "Message: " _
                & ex.Message) 

            ' Whether or not the exception was thrown, the current
            ' thread owns the mutex, and must release it.
            '
            If ex.Mutex IsNot Nothing Then ex.Mutex.ReleaseMutex()            
        End Try

        ' Use two more of the abandoned mutexes for the WaitAll call.
        ' WaitAll doesn't return until all wait handles are signaled,
        ' so the ManualResetEvent must be signaled by calling Set(). 
        _dummy.Set()
        waitFor(1) = _orphan4
        waitFor(2) = _orphan5

        ' The signaled event and the two abandoned mutexes satisfy
        ' the wait condition for WaitAll, but on return it throws
        ' AbandonedMutexException. For WaitAll, the MutexIndex
        ' property is always -1 and the Mutex property is always
        ' Nothing.
        '  
        Try
            WaitHandle.WaitAll(waitFor)
            Console.WriteLine("WaitAll succeeded.")
        Catch ex As AbandonedMutexException
            Console.WriteLine("Exception on return from WaitAll. MutexIndex = " _
                & ex.MutexIndex & "." _
                & vbCrLf & vbTab & "Message: " _
                & ex.Message) 
        Finally
            ' Whether or not the exception was thrown, the current
            ' thread owns the mutexes, and must release them.
            '
            CType(waitFor(1), Mutex).ReleaseMutex()
            CType(waitFor(2), Mutex).ReleaseMutex()
        End Try
    End Sub

    <MTAThread> _
    Public Shared Sub AbandonMutex()
        _orphan1.WaitOne()
        _orphan2.WaitOne()
        _orphan3.WaitOne()
        _orphan4.WaitOne()
        _orphan5.WaitOne()
        ' Abandon the mutexes by exiting without releasing them.
        Console.WriteLine("Thread exits without releasing the mutexes.")
    End Sub
End Class

' This code example produces the following output:
'
'Thread exits without releasing the mutexes.
'Exception on return from WaitOne.
'        Message: The wait completed due to an abandoned mutex.
'Exception on return from WaitAny at index 1.
'        Message: The wait completed due to an abandoned mutex.
'Exception on return from WaitAll. MutexIndex = -1.
'        Message: The wait completed due to an abandoned mutex.

注釈

スレッドがミューテックスを破棄すると、ミューテックスを取得する次のスレッドで例外がスローされます。When a thread abandons a mutex, the exception is thrown in the next thread that acquires the mutex. スレッドは、ミューテックスを既に待機しているか、後でミューテックスに入ったため、ミューテックスを取得する可能性があります。The thread might acquire the mutex because it was already waiting on the mutex or because it enters the mutex at a later time.

破棄ミューテックスは、プログラミングエラーが深刻であることを示します。An abandoned mutex indicates a serious programming error. ミューテックスを解放せずにスレッドが終了すると、ミューテックスによって保護されているデータ構造が一貫性のある状態にならない可能性があります。When a thread exits without releasing the mutex, the data structures protected by the mutex might not be in a consistent state. .NET Framework のバージョン2.0 より前では、中断されたミューテックスの結果として待機が完了した場合に例外がスローされなかったため、このような問題を検出するのは困難でした。Prior to version 2.0 of the .NET Framework, such problems were hard to discover because no exception was thrown if a wait completed as the result of an abandoned mutex. 詳細については、Mutex クラスを参照してください。For more information, see the Mutex class.

次にミューテックスの所有権を要求するスレッドは、この例外を処理し、データ構造の整合性を検証できることを確認してから続行することができます。The next thread to request ownership of the mutex can handle this exception and proceed, provided that the integrity of the data structures can be verified.

コンストラクター

AbandonedMutexException()

既定値を使用して AbandonedMutexException クラスの新しいインスタンスを初期化します。Initializes a new instance of the AbandonedMutexException class with default values.

AbandonedMutexException(Int32, WaitHandle)

放棄されたミューテックスのインデックスを指定する場合はそのインデックスと、ミューテックスを表す AbandonedMutexException オブジェクトを指定して、Mutex クラスの新しいインスタンスを初期化します。Initializes a new instance of the AbandonedMutexException class with a specified index for the abandoned mutex, if applicable, and a Mutex object that represents the mutex.

AbandonedMutexException(SerializationInfo, StreamingContext)

シリアル化されたデータを使用して、AbandonedMutexException クラスの新しいインスタンスを初期化します。Initializes a new instance of the AbandonedMutexException class with serialized data.

AbandonedMutexException(String)

指定したエラー メッセージを使用して、AbandonedMutexException クラスの新しいインスタンスを初期化します。Initializes a new instance of the AbandonedMutexException class with a specified error message.

AbandonedMutexException(String, Exception)

AbandonedMutexException クラスの新しいインスタンスを、指定したエラー メッセージと内部例外を使用して初期化します。Initializes a new instance of the AbandonedMutexException class with a specified error message and inner exception.

AbandonedMutexException(String, Exception, Int32, WaitHandle)

エラー メッセージ、内部例外、放棄されたミューテックスのインデックスを指定する場合はそのインデックス、およびミューテックスを表す AbandonedMutexException オブジェクトを指定して、Mutex クラスの新しいインスタンスを初期化します。Initializes a new instance of the AbandonedMutexException class with a specified error message, the inner exception, the index for the abandoned mutex, if applicable, and a Mutex object that represents the mutex.

AbandonedMutexException(String, Int32, WaitHandle)

エラー メッセージ、放棄されたミューテックスのインデックスを指定する場合はそのインデックス、および放棄されたミューテックスを指定して、AbandonedMutexException クラスの新しいインスタンスを初期化します。Initializes a new instance of the AbandonedMutexException class with a specified error message, the index of the abandoned mutex, if applicable, and the abandoned mutex.

プロパティ

Data

例外に関する追加のユーザー定義情報を提供する、キーと値のペアのコレクションを取得します。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(継承元 Exception)
HelpLink

この例外に関連付けられているヘルプ ファイルへのリンクを取得または設定します。Gets or sets a link to the help file associated with this exception.

(継承元 Exception)
HResult

特定の例外に割り当てられているコード化数値である HRESULT を取得または設定します。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(継承元 Exception)
InnerException

現在の例外の原因となる Exception インスタンスを取得します。Gets the Exception instance that caused the current exception.

(継承元 Exception)
Message

現在の例外を説明するメッセージを取得します。Gets a message that describes the current exception.

(継承元 Exception)
Mutex

例外の原因となった、放棄されたミューテックスがわかっている場合は、そのミューテックスを取得します。Gets the abandoned mutex that caused the exception, if known.

MutexIndex

例外の原因となった、放棄されたミューテックスがわかっている場合は、そのミューテックスのインデックスを取得します。Gets the index of the abandoned mutex that caused the exception, if known.

Source

エラーの原因となるアプリケーションまたはオブジェクトの名前を取得または設定します。Gets or sets the name of the application or the object that causes the error.

(継承元 Exception)
StackTrace

呼び出し履歴で直前のフレームの文字列形式を取得します。Gets a string representation of the immediate frames on the call stack.

(継承元 Exception)
TargetSite

現在の例外がスローされたメソッドを取得します。Gets the method that throws the current exception.

(継承元 Exception)

メソッド

Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(継承元 Object)
GetBaseException()

派生クラスでオーバーライドされた場合、それ以後に発生する 1 つ以上の例外の主要な原因である Exception を返します。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(継承元 Exception)
GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(継承元 Object)
GetObjectData(SerializationInfo, StreamingContext)

派生クラスでオーバーライドされた場合は、その例外に関する情報を使用して SerializationInfo を設定します。When overridden in a derived class, sets the SerializationInfo with information about the exception.

(継承元 Exception)
GetType()

現在のインスタンスのランタイム型を取得します。Gets the runtime type of the current instance.

(継承元 Exception)
MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(継承元 Object)
ToString()

現在の例外の文字列形式を作成して返します。Creates and returns a string representation of the current exception.

(継承元 Exception)

イベント

SerializeObjectState

例外がシリアル化され、例外に関するシリアル化されたデータを含む例外状態オブジェクトが作成されたときに発生します。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(継承元 Exception)

適用対象

こちらもご覧ください