Thread.GetApartmentState Méthode
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Retourne une valeur ApartmentState qui indique l'état de cloisonnement.
public:
System::Threading::ApartmentState GetApartmentState();
public System.Threading.ApartmentState GetApartmentState ();
member this.GetApartmentState : unit -> System.Threading.ApartmentState
Public Function GetApartmentState () As ApartmentState
Une des valeurs ApartmentState qui indique l'état de cloisonnement du thread managé. La valeur par défaut est Unknown.
L’exemple de code suivant illustre les GetApartmentStateméthodes , SetApartmentStateet TrySetApartmentState . L’exemple de code crée un thread. Avant le démarrage du thread, GetApartmentState affiche l’état initial ApartmentState.Unknown et SetApartmentState remplace l’état par ApartmentState.STA. La TrySetApartmentState méthode retourne false
ensuite lors de la tentative de modification de l’état en ApartmentState.MTA , car l’état d’appartement est déjà défini. Si la même opération avait été tentée avec SetApartmentState, InvalidOperationException aurait été levée.
Une fois le thread démarré, la TrySetApartmentState méthode est à nouveau utilisée. Cette fois, il lève ThreadStateException , car le thread a déjà été démarré.
using namespace System;
using namespace System::Threading;
void ThreadProc()
{
Thread::Sleep(2000);
};
void main()
{
Thread^ t = gcnew Thread(gcnew ThreadStart(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();
}
/* 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.
*/
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.
*/
open System.Threading
let threadProc () = Thread.Sleep 2000
let t = Thread threadProc
printfn $"Before setting apartment state: {t.GetApartmentState()}"
t.SetApartmentState ApartmentState.STA
printfn $"After setting apartment state: {t.GetApartmentState()}"
let result = t.TrySetApartmentState ApartmentState.MTA
printfn $"Try to change state: {result}"
t.Start()
Thread.Sleep 500
try
t.TrySetApartmentState ApartmentState.STA |> ignore
with :? ThreadStateException ->
printfn "ThreadStateException occurs if apartment state is set after starting thread."
t.Join()
// 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.
Imports System.Threading
Module Example
Sub Main()
Dim t As New Thread(AddressOf ThreadProc)
Console.WriteLine("Before setting apartment state: {0}", _
t.GetApartmentState())
t.SetApartmentState(ApartmentState.STA)
Console.WriteLine("After setting apartment state: {0}", _
t.GetApartmentState())
Dim result As Boolean = _
t.TrySetApartmentState(ApartmentState.MTA)
Console.WriteLine("Try to change state: {0}", result)
t.Start()
Thread.Sleep(500)
Try
t.TrySetApartmentState(ApartmentState.STA)
Catch ex As ThreadStateException
Console.WriteLine("ThreadStateException occurs " & _
"if apartment state is set after starting thread.")
End Try
t.Join()
End Sub
Sub ThreadProc()
Thread.Sleep(2000)
End Sub
End Module
' 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.
Cette méthode, ainsi que la SetApartmentState méthode et la TrySetApartmentState méthode, remplacent la ApartmentState propriété .
Produit | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :