AbandonedMutexException Klasse
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Die Ausnahme, die ausgelöst wird, wenn ein Thread ein Mutex Objekt erhält, das ein anderer Thread verlassen hat, indem er beendet wird, ohne ihn freizugeben.
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
- Vererbung
- Vererbung
- Attribute
Beispiele
Im folgenden Codebeispiel wird ein Thread ausgeführt, der fünf Mutexes aufgibt und deren Auswirkungen auf die WaitOneMethoden und WaitAllWaitAnyMethoden veranschaulicht. Der Wert der MutexIndex Eigenschaft wird für den WaitAny Aufruf angezeigt.
Hinweis
Der Aufruf der WaitAny Methode wird durch einen der abgebrochenen Mutexes unterbrochen. Die andere verlassene Mutex könnte dennoch dazu führen AbandonedMutexException , dass sie durch nachfolgende Wartemethoden ausgelöst wird.
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.
Hinweise
Wenn ein Thread einen Mutex verlässt, wird die Ausnahme im nächsten Thread ausgelöst, der den Mutex erhält. Der Thread kann den Mutex abrufen, da er bereits auf den Mutex wartete, oder weil er zu einem späteren Zeitpunkt in den Mutex eingeht.
Ein verlassener Mutex weist auf einen schwerwiegenden Programmierfehler hin. Wenn ein Thread beendet wird, ohne den Mutex freizugeben, befinden sich die durch den Mutex geschützten Datenstrukturen möglicherweise nicht in einem konsistenten Zustand. Vor Version 2.0 von .NET Framework waren solche Probleme schwer zu erkennen, da keine Ausnahme ausgelöst wurde, wenn eine Wartezeit als Ergebnis eines abgebrochenen Mutex abgeschlossen wurde. Weitere Informationen finden Sie in der Mutex Klasse.
Der nächste Thread zum Anfordern des Besitzes des Mutex kann diese Ausnahme behandeln und fortfahren, vorausgesetzt, die Integrität der Datenstrukturen kann überprüft werden.
Konstruktoren
| Name | Beschreibung |
|---|---|
| AbandonedMutexException() |
Initialisiert eine neue Instanz der AbandonedMutexException Klasse mit Standardwerten. |
| AbandonedMutexException(Int32, WaitHandle) |
Initialisiert eine neue Instanz der AbandonedMutexException Klasse mit einem angegebenen Index für das verlassene Mutex, falls zutreffend, und ein Mutex Objekt, das den Mutex darstellt. |
| AbandonedMutexException(SerializationInfo, StreamingContext) |
Veraltet.
Initialisiert eine neue Instanz der AbandonedMutexException Klasse mit serialisierten Daten. |
| AbandonedMutexException(String, Exception, Int32, WaitHandle) |
Initialisiert eine neue Instanz der AbandonedMutexException Klasse mit einer angegebenen Fehlermeldung, der inneren Ausnahme, dem Index für das verlassene Mutex, falls zutreffend, und einem Mutex Objekt, das den Mutex darstellt. |
| AbandonedMutexException(String, Exception) |
Initialisiert eine neue Instanz der AbandonedMutexException Klasse mit einer angegebenen Fehlermeldung und einer inneren Ausnahme. |
| AbandonedMutexException(String, Int32, WaitHandle) |
Initialisiert eine neue Instanz der AbandonedMutexException Klasse mit einer angegebenen Fehlermeldung, dem Index des verlassenen Mutex, falls zutreffend, und dem verlassenen Mutex. |
| AbandonedMutexException(String) |
Initialisiert eine neue Instanz der AbandonedMutexException Klasse mit einer angegebenen Fehlermeldung. |
Eigenschaften
| Name | Beschreibung |
|---|---|
| Data |
Ruft eine Auflistung von Schlüssel-Wert-Paaren ab, die zusätzliche benutzerdefinierte Informationen zur Ausnahme bereitstellen. (Geerbt von Exception) |
| HelpLink |
Dient zum Abrufen oder Festlegen eines Links zur Hilfedatei, die dieser Ausnahme zugeordnet ist. (Geerbt von Exception) |
| HResult |
Dient zum Abrufen oder Festlegen von HRESULT, einem codierten numerischen Wert, der einer bestimmten Ausnahme zugewiesen ist. (Geerbt von Exception) |
| InnerException |
Ruft die Exception Instanz ab, die die aktuelle Ausnahme verursacht hat. (Geerbt von Exception) |
| Message |
Ruft eine Nachricht ab, die die aktuelle Ausnahme beschreibt. (Geerbt von Exception) |
| Mutex |
Ruft den verlassenen Mutex ab, der die Ausnahme verursacht hat, falls bekannt. |
| MutexIndex |
Ruft den Index des verlassenen Mutex ab, der die Ausnahme verursacht hat, falls bekannt. |
| Source |
Dient zum Abrufen oder Festlegen des Namens der Anwendung oder des Objekts, das den Fehler verursacht. (Geerbt von Exception) |
| StackTrace |
Ruft eine Zeichenfolgendarstellung der unmittelbaren Frames im Aufrufstapel ab. (Geerbt von Exception) |
| TargetSite |
Ruft die Methode ab, die die aktuelle Ausnahme auslöst. (Geerbt von Exception) |
Methoden
| Name | Beschreibung |
|---|---|
| Equals(Object) |
Bestimmt, ob das angegebene Objekt dem aktuellen Objekt entspricht. (Geerbt von Object) |
| GetBaseException() |
Wenn sie in einer abgeleiteten Klasse überschrieben wird, wird die Exception Ursache einer oder mehrerer nachfolgenden Ausnahmen zurückgegeben. (Geerbt von Exception) |
| GetHashCode() |
Dient als Standardhashfunktion. (Geerbt von Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Veraltet.
Wenn sie in einer abgeleiteten Klasse überschrieben wird, werden die SerializationInfo Informationen zur Ausnahme festgelegt. (Geerbt von Exception) |
| GetType() |
Ruft den Laufzeittyp der aktuellen Instanz ab. (Geerbt von Exception) |
| MemberwiseClone() |
Erstellt eine flache Kopie der aktuellen Object. (Geerbt von Object) |
| ToString() |
Erstellt und gibt eine Zeichenfolgendarstellung der aktuellen Ausnahme zurück. (Geerbt von Exception) |
Ereignisse
| Name | Beschreibung |
|---|---|
| SerializeObjectState |
Veraltet.
Tritt auf, wenn eine Ausnahme serialisiert wird, um ein Ausnahmestatusobjekt zu erstellen, das serialisierte Daten zu der Ausnahme enthält. (Geerbt von Exception) |