Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Thread.ApartmentState-Eigenschaft

 
Hinweis: Diese API ist jetzt veraltet.

Ruft den Apartmentzustand dieses Threads ab oder legt diesen fest.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

[ObsoleteAttribute("The ApartmentState property has been deprecated.  Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", 
	false)]
public ApartmentState ApartmentState {
	get;
	[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
		SelfAffectingThreading = true)]
	set;
}

Eigenschaftswert

Type: System.Threading.ApartmentState

Einer der ApartmentState-Werte. Der Anfangswert ist Unknown.

Exception Condition
ArgumentException

Es wird versucht, diese Eigenschaft in einen Zustand zu versetzen, der kein gültiger Apartmentzustand ist, d. h. in einen anderen Zustand als Singlethread-Apartment (STA) oder Multithread-Apartment (MTA).

The ApartmentState property is obsolete. The non-obsolete alternatives are the GetApartmentState method to retrieve the apartment state and the SetApartmentState method to set the apartment state.

In the .NET Framework versions 1.0 and 1.1, the ApartmentState property marks a thread to indicate that it will execute in a single-threaded or multithreaded apartment. This property can be set when the thread is in the Unstarted or Running thread state; however, it can be set only once for a thread. If the property has not been set, it returns Unknown.

An attempt to use the P:System.Threading.Thread.ApartmentState property to set the apartment state of a thread whose apartment state has already been set is ignored. However, the M:System.Threading.Thread.SetApartmentState(System.Threading.ApartmentState) method throws a T:System.InvalidOperationException in this case.

System_CAPS_importantWichtig

In the .NET Framework version 2.0, new threads are initialized as F:System.Threading.ApartmentState.MTA if their apartment state has not been set before they are started. The main application thread is initialized to F:System.Threading.ApartmentState.MTA by default. You can no longer set the main application thread to F:System.Threading.ApartmentState.STA by setting the T:System.Threading.ApartmentState property on the first line of code. Use the T:System.STAThreadAttribute instead.

In the .NET Framework version 2.0, you can specify the COM threading model for a C++ application using the /CLRTHREADATTRIBUTE (Set CLR Thread Attribute) linker option.

The following code example demonstrates how to set the apartment state of a thread.

using System;
using System.Threading;

class ApartmentTest
{
    static void Main()
    {
        Thread newThread = 
            new Thread(new ThreadStart(ThreadMethod));
        newThread.SetApartmentState(ApartmentState.MTA);

        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", 
            newThread.ThreadState, newThread.ApartmentState);

        newThread.Start();

        // Wait for newThread to start and go to sleep.
        Thread.Sleep(300);
        try
        {
            // This causes an exception since newThread is sleeping.
            newThread.SetApartmentState(ApartmentState.STA);
        }
        catch(ThreadStateException stateException)
        {
            Console.WriteLine("\n{0} caught:\n" +
                "Thread is not in the Unstarted or Running state.", 
                stateException.GetType().Name);
            Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                newThread.ThreadState, newThread.GetApartmentState());
        }
    }

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    }
}

.NET Framework
Verfügbar seit 1.1
Zurück zum Anfang
Anzeigen: