Thread.GetData メソッドとは? わかりやすく解説

Thread.GetData メソッド

現在のスレッド現在のドメイン内で指定した現在のスレッドスロットから値を取得します

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

Public Shared Function GetData
 ( _
    slot As LocalDataStoreSlot _
) As Object
Dim slot As LocalDataStoreSlot
Dim returnValue As Object

returnValue = Thread.GetData(slot)
public static Object GetData (
    LocalDataStoreSlot slot
)
public:
static Object^ GetData (
    LocalDataStoreSlot^ slot
)
public static Object GetData (
    LocalDataStoreSlot slot
)
public static function GetData
 (
    slot : LocalDataStoreSlot
) : Object

パラメータ

slot

値の取得元の LocalDataStoreSlot。

戻り値
取得された値。

解説解説
メモメモ

このメソッド適用される HostProtectionAttribute 属性Resources プロパティの値は、SharedState または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的にはアイコンダブルクリックコマンド入力、またはブラウザURL入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラストピックまたは「SQL Server プログラミングホスト保護属性」を参照してください

スレッドは、ローカル ストア メモリ機構使用してスレッド固有のデータ格納します共通言語ランタイムは、各プロセス作成時に、そのプロセスにマルチスロットのデータ ストア配列割り当てますスレッドは、データ ストアデータ スロット割り当てたりスロットデータ値を格納および取得したり、スレッド有効期限切れた後でスロット再利用できるようにスロット解放したできますデータ スロットスレッドごとに一意です。他のスレッドは (子スレッドであっても) そのデータ取得できません。

使用例使用例

データ スロットスレッド固有の情報格納する方法の例を次に示します

Imports System
Imports System.Threading

Class Test

    <MTAThread> _
    Shared Sub Main()
        Dim newThreads(3) As Thread
        For i As Integer
 = 0 To newThreads.Length - 1
            newThreads(i) = New Thread(AddressOf
 Slot.SlotTest)
            newThreads(i).Start()
        Next i
    End Sub

End Class

Public Class Slot

    Shared randomGenerator As Random
    Shared localSlot As LocalDataStoreSlot

    Shared Sub New()
        randomGenerator = new Random()
        localSlot = Thread.AllocateDataSlot()
    End Sub

    Shared Sub SlotTest()

        ' Set different data in each thread's data slot.
        Thread.SetData(localSlot, randomGenerator.Next(1, 200))

        ' Write the data from each thread's data slot.
        Console.WriteLine("Data in thread_{0}'s
 data slot: {1,3}", _
            AppDomain.GetCurrentThreadId().ToString(), _
            Thread.GetData(localSlot).ToString())

        ' Allow other threads time to execute SetData to show
        ' that a thread's data slot is unique to the thread.
        Thread.Sleep(1000)

        ' Write the data from each thread's data slot.
        Console.WriteLine("Data in thread_{0}'s
 data slot: {1,3}", _
            AppDomain.GetCurrentThreadId().ToString(), _
            Thread.GetData(localSlot).ToString())
    End Sub

End Class
using System;
using System.Threading;

class Test
{
    static void Main()
    {
        Thread[] newThreads = new Thread[4];
        for(int i = 0; i < newThreads.Length;
 i++)
        {
            newThreads[i] = new Thread(
                new ThreadStart(Slot.SlotTest));
            newThreads[i].Start();
        }
    }
}

class Slot
{
    static Random randomGenerator;
    static LocalDataStoreSlot localSlot;

    static Slot()
    {
        randomGenerator = new Random();
        localSlot = Thread.AllocateDataSlot();
    }

    public static void SlotTest()
    {
        // Set different data in each thread's data slot.
        Thread.SetData(localSlot, randomGenerator.Next(1, 200));

        // Write the data from each thread's data slot.
        Console.WriteLine("Data in thread_{0}'s data slot:
 {1,3}", 
            AppDomain.GetCurrentThreadId().ToString(),
            Thread.GetData(localSlot).ToString());

        // Allow other threads time to execute SetData to show
        // that a thread's data slot is unique to the thread.
        Thread.Sleep(1000);

        Console.WriteLine("Data in thread_{0}'s data slot:
 {1,3}", 
            AppDomain.GetCurrentThreadId().ToString(),
            Thread.GetData(localSlot).ToString());
    }
}
using namespace System;
using namespace System::Threading;
ref class Slot
{
private:
   static Random^ randomGenerator;
   static LocalDataStoreSlot^ localSlot;
   static Slot()
   {
      randomGenerator = gcnew Random;
      localSlot = Thread::AllocateDataSlot();
   }


public:
   static void SlotTest()
   {
      
      // Set different data in each thread's data slot.
      Thread::SetData( localSlot, randomGenerator->Next( 1, 200 ) );
      
      // Write the data from each thread's data slot.
      Console::WriteLine( "Data in thread_{0}'s data slot:
 {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData( localSlot
 )->ToString() );
      
      // Allow other threads time to execute SetData to show
      // that a thread's data slot is unique to the thread.
      Thread::Sleep( 1000 );
      Console::WriteLine( "Data in thread_{0}'s data slot:
 {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData( localSlot
 )->ToString() );
   }

};

int main()
{
   array<Thread^>^newThreads = gcnew array<Thread^>(4);
   for ( int i = 0; i < newThreads->Length;
 i++ )
   {
      newThreads[ i ] = gcnew Thread( gcnew ThreadStart( &Slot::SlotTest ) );
      newThreads[ i ]->Start();

   }
}

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

class Test
{
    public static void main(String[]
 args)
    {
        Thread newThreads[] = new Thread[4];

        for (int i = 0; i < newThreads.length;
 i++) {
            newThreads[i] = new Thread(new
 ThreadStart(Slot.SlotTest));
            newThreads[i].Start();
        }
    } //main
} //Test

class Slot
{
    private static Random randomGenerator;

    private static LocalDataStoreSlot localSlot;
    static 
    {
        randomGenerator = new Random();
        localSlot = Thread.AllocateDataSlot();
    } //static 

    public static void SlotTest()
    {
        // Set different data in each thread's data slot.
        Thread.SetData(localSlot, new Integer(randomGenerator.Next(1,
 200)));

        // Write the data from each thread's data slot.
        Console.WriteLine("Data in thread_{0}'s data slot:
 {1,3}",
            String.valueOf(AppDomain.GetCurrentThreadId()),
            Thread.GetData(localSlot).toString());

        // Allow other threads time to execute SetData to show
        // that a thread's data slot is unique to the thread.
        Thread.Sleep(1000);
        Console.WriteLine("Data in thread_{0}'s data slot:
 {1,3}",
            String.valueOf(AppDomain.GetCurrentThreadId()),
            Thread.GetData(localSlot).toString());
    } //SlotTest
} //Slot
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からThread.GetData メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からThread.GetData メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からThread.GetData メソッド を検索

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

辞書ショートカット

すべての辞書の索引

「Thread.GetData メソッド」の関連用語

Thread.GetData メソッドのお隣キーワード
検索ランキング

   

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



Thread.GetData メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS