AbandonedMutexException (Clase)

AbandonedMutexException (Clase)

Excepción que se produce cuando un subproceso adquiere un objeto Mutex que otro subproceso ha abandonado al salir sin liberarlo.

Espacio de nombres: System.Threading
Ensamblado: mscorlib (en mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(false)] 
public class AbandonedMutexException : SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(false) */ 
public class AbandonedMutexException extends SystemException
SerializableAttribute 
ComVisibleAttribute(false) 
public class AbandonedMutexException extends SystemException
No aplicable.

Cuando un subproceso abandona una exclusión mutua, la excepción se produce en el subproceso siguiente que adquiere la exclusión mutua. El subproceso podría adquirir la exclusión mutua porque ya estaba esperando en ella o porque ha entrado posteriormente en la misma.

Una exclusión mutua abandonada indica un error de programación grave. Cuando un subproceso sale sin liberar la exclusión mutua, el estado de las estructuras de datos protegidas por la exclusión mutua podría no ser coherente. Antes de la versión 2.0 de .NET Framework, era difícil descubrir tales problemas porque cuando una espera finalizaba por el abandono de una exclusión mutua no se producía ninguna excepción. Para obtener más información, vea la clase Mutex.

El siguiente subproceso que solicita la propiedad de la exclusión mutua puede controlar esta excepción y continuar, siempre que se pueda comprobar la integridad de las estructuras de datos.

En el ejemplo de código siguiente se ejecuta un subproceso que abandona cinco exclusiones mutuas, mostrando sus efectos en los métodos WaitOne, WaitAny y WaitAll. Se muestra el valor de la propiedad MutexIndex para la llamada a WaitAny.

NotaNota:

Una de las exclusiones mutuas abandonadas interrumpe la llamada al método WaitAny. La otra exclusión mutua abandonada aún puede provocar que los métodos de espera subsiguientes produzcan una excepción AbandonedMutexException.


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.
 */

import System.*;
import 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();

    /** @attribute MTAThread()
     */
    public static void main(String[] args)
    {
        // Start a thread that takes all five mutexes, and then
        // ends without releasing them.
        //
        System.Threading.Thread t = new System.Threading.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.get_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
        // ManualReset_Event 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.");
            if (waitFor.get_Item(index) instanceof Mutex) {
                ((Mutex)(waitFor.get_Item(index))).ReleaseMutex();
            }
        }
        catch (AbandonedMutexException ex) {
            Console.WriteLine("Exception on return from WaitAny at index {0}" 
                + "\r\n\tMessage: {1}", (Int32)ex.get_MutexIndex(),
                ex.get_Message());

            // Whether or not the exception was thrown, the current
            // thread owns the mutex, and must release it.
            //
            if (ex.get_Mutex() != null) ex.get_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 ManualReset_Event must be signaled by calling set_().
        dummy.Set();
        waitFor.set_Item(1, orphan4);
        waitFor.set_Item(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 WaitAny at index {0}" 
                + "\r\n\tMessage: {1}", (Int32)ex.get_MutexIndex(),
                ex.get_Message());
        }
        finally {
            // Whether or not the exception was thrown, the current
            // thread owns the mutexes, and must release them.
            //
            orphan4.ReleaseMutex();
            orphan5.ReleaseMutex();
        }
    } //main

    /** @attribute 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.");
    } //AbandonMutex
} //Example

/* 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.
 */

System.Object
   System.Exception
     System.SystemException
      System.Threading.AbandonedMutexException

Los miembros estáticos públicos (Shared en Visual Basic) de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

Windows 98, Windows 2000 Service Pack 4, Windows Millennium, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0, 2.0

Adiciones de comunidad

AGREGAR
Mostrar:
© 2016 Microsoft