AbandonedMutexException 类
定义
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
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type AbandonedMutexException = class
inherit SystemException
Public Class AbandonedMutexException
Inherits Exception
Public Class AbandonedMutexException
Inherits SystemException
- 继承
- 继承
- 属性
示例
下面的代码示例执行一个丢弃五个互斥体的线程,并演示它们对 WaitOne 、 WaitAny 和 WaitAll 方法的影响。The following code example executes a thread that abandons five mutexes, demonstrating their effects on the WaitOne, WaitAny, and WaitAll methods. 为 MutexIndex 该调用显示属性的值 WaitAny 。The value of the MutexIndex property is displayed for the WaitAny call.
备注
对方法的调用 WaitAny 被一个被放弃的 mutex 中断。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.
放弃的 mutex 表明出现了严重的编程错误。An abandoned mutex indicates a serious programming error. 当线程在不释放 mutex 的情况下退出时,由 mutex 保护的数据结构可能不是一致的状态。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 版之前,此类问题很难发现,因为如果在被放弃的 mutex 的结果中完成等待,则不会引发异常。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.
如果可以验证数据结构的完整性,下一个请求 mutex 所有权的线程就可以处理此异常并继续进行。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() |
当在派生类中重写时,返回 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) |