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)
| Name | Description | |
|---|---|---|
![]() | AbandonedMutexException() | Initializes a new instance of the AbandonedMutexException class with default values. |
![]() | 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) | Initializes a new instance of the AbandonedMutexException class with a specified error message. |
![]() | AbandonedMutexException(String, Exception) | Initializes a new instance of the AbandonedMutexException class with a specified error message and inner exception. |
![]() | 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. |
![]() | 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. |
| 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 | |
![]() | 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() | |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetObjectData(SerializationInfo, StreamingContext) | 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() | |
![]() | 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. |
Option Explicit Imports System 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.
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.




