AbandonedMutexException Class
The exception that is thrown when one thread acquires a Mutex object that another thread has abandoned by exiting without releasing it.
Assembly: mscorlib (in mscorlib.dll)
The AbandonedMutexException type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | AbandonedMutexException() | Initializes a new instance of the AbandonedMutexException class with default values. |
![]() | AbandonedMutexException(String) | Initializes a new instance of the AbandonedMutexException class with a specified error message. |
![]() | AbandonedMutexException(Int32, WaitHandle) | 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) | Initializes a new instance of the AbandonedMutexException class with serialized data. |
![]() | AbandonedMutexException(String, Exception) | Initializes a new instance of the AbandonedMutexException class with a specified error message and inner exception. |
![]() | AbandonedMutexException(String, Int32, WaitHandle) | 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. |
![]() | AbandonedMutexException(String, Exception, Int32, WaitHandle) | 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. |
| Name | Description | |
|---|---|---|
![]() | Data | Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.) |
![]() | HelpLink | Gets or sets a link to the help file associated with this exception. (Inherited from Exception.) |
![]() | HResult | Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.) |
![]() | InnerException | Gets the Exception instance that caused the current exception. (Inherited from Exception.) |
![]() | Message | Gets a message that describes the current exception. (Inherited from 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. (Inherited from Exception.) |
![]() | StackTrace | Gets a string representation of the immediate frames on the call stack. (Inherited from Exception.) |
![]() | TargetSite | Gets the method that throws the current exception. (Inherited from Exception.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetBaseException | When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetObjectData | When overridden in a derived class, sets the SerializationInfo with information about the exception. (Inherited from Exception.) |
![]() | GetType | Gets the runtime type of the current instance. (Inherited from Exception.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Creates and returns a string representation of the current exception. (Inherited from Exception.) |
| Name | Description | |
|---|---|---|
![]() | SerializeObjectState | Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception.) |
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. 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. 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.
The following code example executes a thread that abandons five mutexes, demonstrating their effects on the WaitOne, WaitAny, and WaitAll methods. The value of the MutexIndex property is displayed for the WaitAny call.
Note |
|---|
The call to the WaitAny method is interrupted by one of the abandoned mutexes. 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.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

