GetApartmentState Método
Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Thread.GetApartmentState (Método) ()

 

Devuelve un valor ApartmentState que indica el estado del apartamento.

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

public ApartmentState GetApartmentState()

Valor devuelto

Type: System.Threading.ApartmentState

Uno de los valores de ApartmentState que indica el estado del apartamento del subproceso administrado. De manera predeterminada, es ApartmentState.Unknown.

This method, along with the M:System.Threading.Thread.SetApartmentState(System.Threading.ApartmentState) method and the M:System.Threading.Thread.TrySetApartmentState(System.Threading.ApartmentState) method, replaces the P:System.Threading.Thread.ApartmentState property.

The following code example demonstrates the M:System.Threading.Thread.GetApartmentState, M:System.Threading.Thread.SetApartmentState(System.Threading.ApartmentState), and M:System.Threading.Thread.TrySetApartmentState(System.Threading.ApartmentState) methods. The code example creates a thread. Before the thread is started, M:System.Threading.Thread.GetApartmentState displays the initial F:System.Threading.ApartmentState.Unknown state and M:System.Threading.Thread.SetApartmentState(System.Threading.ApartmentState) changes the state to F:System.Threading.ApartmentState.STA. The M:System.Threading.Thread.TrySetApartmentState(System.Threading.ApartmentState) method then returns false when attempting to change the state to F:System.Threading.ApartmentState.MTA because the apartment state is already set. If the same operation had been attempted with M:System.Threading.Thread.SetApartmentState(System.Threading.ApartmentState), T:System.InvalidOperationException would have been thrown.

After the thread is started, the M:System.Threading.Thread.TrySetApartmentState(System.Threading.ApartmentState) method is used again. This time it throws T:System.Threading.ThreadStateException because the thread has already been started.

using System;
using System.Threading;

class Example
{
    public static void Main()
    {
        Thread t = new Thread(ThreadProc);
        Console.WriteLine("Before setting apartment state: {0}", 
            t.GetApartmentState());

        t.SetApartmentState(ApartmentState.STA);
        Console.WriteLine("After setting apartment state: {0}", 
            t.GetApartmentState());

        bool result = t.TrySetApartmentState(ApartmentState.MTA);
        Console.WriteLine("Try to change state: {0}", result);

        t.Start();

        Thread.Sleep(500);

        try
        {
            t.TrySetApartmentState(ApartmentState.STA);
        }
        catch (ThreadStateException)
        {
            Console.WriteLine("ThreadStateException occurs " +
                "if apartment state is set after starting thread.");
        }

        t.Join();
    }

    public static void ThreadProc()
    {
        Thread.Sleep(2000);
    }
}

/* This code example produces the following output:

Before setting apartment state: Unknown
After setting apartment state: STA
Try to change state: False
ThreadStateException occurs if apartment state is set after starting thread.
 */

.NET Framework
Disponible desde 2.0
Volver al principio
Mostrar:
© 2016 Microsoft