ApartmentState 列挙体とは? わかりやすく解説

ApartmentState 列挙体

Threadアパートメント状態を指定します

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration ApartmentState
Dim instance As ApartmentState
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum ApartmentState
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum class ApartmentState
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum ApartmentState
SerializableAttribute 
ComVisibleAttribute(true) 
public enum ApartmentState
メンバメンバ
解説解説

アパートメントとは、同じスレッド アクセス要件共有するオブジェクトプロセス内の論理コンテナです。同じアパートメント内のオブジェクトはすべて、そのアパートメント内のどのスレッドからの呼び出し受け取ることができます.NET Frameworkアパートメント使用せずマネージ オブジェクトスレッド セーフ方法すべての共有リソース独自に使用します

COM クラスアパートメント使用するため、共通言語ランタイムでは、COM 相互運用状況COM オブジェクト呼び出すときにアパートメント作成および初期化する必要がありますマネージ スレッドでは、1 つスレッドだけを格納できるシングルスレッド アパートメント (STA: single-threaded apartment)、または 1 つ上のスレッド格納するマルチスレッド アパートメント (MTA: multithreaded apartment) を作成および入力できます作成するアパートメント種類制御するには、スレッドの ApartmentState プロパティApartmentState 列挙体の値の 1 つ設定します1 つスレッドでは COM アパートメント一度しか初期化できないため、アンマネージ コード一度呼び出すと、アパートメント種類変更できません。

詳細については、「Thread」、「マネージ スレッドとアンマネージ スレッド」、および「高度な COM 相互運用性」を参照してください

使用例使用例

スレッドアパートメントの状態を設定する方法の例を次に示します

Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class ApartmentTest

    <MTAThread> _
    Shared Sub Main()
    
        Dim newThread As Thread = New
 Thread(AddressOf ThreadMethod)
        newThread.SetApartmentState(ApartmentState.MTA)

        ' The following line is ignored since 
        ' ApartmentState can only be set once.
        newThread.SetApartmentState(ApartmentState.STA)

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

        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 stateException As ThreadStateException
            Console.WriteLine(vbCrLf & "{0} caught:"
 & vbCrLf & _
                "Thread is not In the Unstarted or Running state.",
 _
                stateException.GetType().Name)
            Console.WriteLine("ThreadState: {0}, ApartmentState:
 " & _
                "{1}", newThread.ThreadState, newThread.GetApartmentState())
        End Try

    End Sub

    Shared Sub ThreadMethod()
        Thread.Sleep(1000)
    End Sub

End Class
using System;
using System.Threading;

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

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread.SetApartmentState(ApartmentState.STA);

        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);
    }
}
using namespace System;
using namespace System::Threading;
ref class ApartmentTest
{
public:
   static void ThreadMethod()
   {
      Thread::Sleep( 1000 );
   }

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( &ApartmentTest::ThreadMethod
 ) );
   newThread->SetApartmentState(ApartmentState::MTA);
   
   // The following line is ignored since 
   // ApartmentState can only be set once.
   newThread->SetApartmentState(ApartmentState::STA);
   Console::WriteLine( "ThreadState: {0}, ApartmentState: {1}", newThread->ThreadState.ToString(),
 newThread->GetApartmentState().ToString() );
   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.ToString(),
 newThread->GetApartmentState().ToString() );
   }

}

import System.*;
import System.Threading.*;
import System.Threading.Thread;    

class ApartmentTest
{
    public static void main(String[]
 args)
    {
        Thread newThread = new Thread(new ThreadStart(ThreadMethod));

        newThread.set_ApartmentState(ApartmentState.MTA);

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread.set_ApartmentState(ApartmentState.STA);
        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                        newThread.get_ThreadState(),
                        newThread.get_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.set_ApartmentState(ApartmentState.STA);
        }
        catch (ThreadStateException stateException) {
            Console.WriteLine("\n{0} caught:\n" + 
                "Thread is not in the Unstarted or Running
 state.", 
                stateException.GetType().get_Name());
            Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                newThread.get_ThreadState(),newThread.get_ApartmentState());
        }
    } //main

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    } //ThreadMethod
} //ApartmentTest
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

ApartmentState 列挙体のお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ApartmentState 列挙体のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS