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

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Thread.FreeNamedDataSlot メソッドの意味・解説 

Thread.FreeNamedDataSlot メソッド

プロセス内のすべてのスレッドに関して、名前とスロットの関連付けを解除します。

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

Public Shared Sub FreeNamedDataSlot
 ( _
    name As String _
)
Dim name As String

Thread.FreeNamedDataSlot(name)
public static void FreeNamedDataSlot
 (
    string name
)
public:
static void FreeNamedDataSlot (
    String^ name
)
public static void FreeNamedDataSlot
 (
    String name
)
public static function FreeNamedDataSlot
 (
    name : String
)

パラメータ

name

解放されるデータ スロットの名前。

解説解説
メモメモ

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

いずれかのスレッドが FreeNamedDataSlot を呼び出した後、別のスレッドが同じ名前で GetNamedDataSlot を呼び出すと、その名前と関連付けられた新しいスロットが割り当てられます。その後、いずれかのスレッドが GetNamedDataSlot を呼び出すと、この新しいスロットが返されます。ただし、以前の GetNamedDataSlot の呼び出しで返された System.LocalDataStoreSlot を持っているスレッドは、継続して古いスロットを使用できます。

名前と関連付けられているスロットは、FreeNamedDataSlot の呼び出し前に取得された LocalDataStoreSlot がすべて解放され、ガベージ コレクションされたときにだけ解放されます。

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

使用例使用例

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

Option Explicit
Option Strict

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
 SlotExample.SlotTest)
            newThreads(i).Start()
        Next i
    End Sub
    
End Class

Public Class SlotExample

    Shared randomGenerator As Random = New
 Random()

    Shared Sub SlotTest()
    
        ' Set different data in each thread's data slot.
        Thread.SetData( _
            Thread.GetNamedDataSlot("Random"), _ 
            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( _
            Thread.GetNamedDataSlot("Random")).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 is still: {1,3}", _
            AppDomain.GetCurrentThreadId().ToString(), _
            Thread.GetData( _
            Thread.GetNamedDataSlot("Random")).ToString())

        ' Allow time for other threads to show their data,
        ' then demonstrate that any code a thread executes
        ' has access to the thread's named data slot.        
        Thread.Sleep(1000)

        Dim o As New Other()
        o.ShowSlotData()
    End Sub
    
End Class

Public Class Other
    Public Sub ShowSlotData()
        ' This method has no access to the data in the SlotExample
        ' class, but when executed by a thread it can obtain
        ' the thread's data from a named slot.
        Console.WriteLine("Other code displays data in thread_{0}'s
 data slot: {1,3}", _
            AppDomain.GetCurrentThreadId().ToString(), _
            Thread.GetData( _
            Thread.GetNamedDataSlot("Random")).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 = new Random();

    public static void SlotTest()
    {
        // Set different data in each thread's data slot.
        Thread.SetData(
            Thread.GetNamedDataSlot("Random"), 
            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(
            Thread.GetNamedDataSlot("Random")).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
 is still: {1,3}", 
            AppDomain.GetCurrentThreadId().ToString(),
            Thread.GetData(
            Thread.GetNamedDataSlot("Random")).ToString());

        // Allow time for other threads to show their data,
        // then demonstrate that any code a thread executes
        // has access to the thread's named data slot.        
        Thread.Sleep(1000);

        Other o = new Other();
        o.ShowSlotData();
    }
}

public class Other
{
    public void ShowSlotData()
    {
        // This method has no access to the data in the Slot
        // class, but when executed by a thread it can obtain
        // the thread's data from a named slot.
        Console.WriteLine("Other code displays data in thread_{0}'s
 data slot: {1,3}", 
            AppDomain.GetCurrentThreadId().ToString(), 
            Thread.GetData( 
            Thread.GetNamedDataSlot("Random")).ToString());
    }
}
using namespace System;
using namespace System::Threading;
ref class Other
{
public:
   void ShowSlotData()
   {
      
      // This method has no access to the data in the Slot
      // class, but when executed by a thread it can obtain
      // the thread's data from a named slot.
      Console::WriteLine(  "Other code displays data in thread_{0}'s
 data slot: {1,3}", AppDomain::GetCurrentThreadId(), Thread::GetData( Thread::GetNamedDataSlot(
  "Random" ) )->ToString() );
   }

};

ref class Slot
{
private:
   static Random^ randomGenerator = gcnew Random;

public:
   static void SlotTest()
   {
      
      // Set different data in each thread's data slot.
      Thread::SetData( Thread::GetNamedDataSlot( "Random" ), 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( Thread::GetNamedDataSlot(
 "Random" ) )->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
 is still: {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData(
 Thread::GetNamedDataSlot( "Random" ) )->ToString() );
      
      // Allow time for other threads to show their data,
      // then demonstrate that any code a thread executes
      // has access to the thread's named data slot.        
      Thread::Sleep( 1000 );
      Other^ o = gcnew Other;
      o->ShowSlotData();
   }

};

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 =
 new Random();

    public static void SlotTest()
    {
        // Set different data in each thread's data slot.
        Thread.SetData(Thread.GetNamedDataSlot("Random"), 
            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(Thread.GetNamedDataSlot("Random")).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
 is still: {1,3}",
            String.valueOf(AppDomain.GetCurrentThreadId()), 
            Thread.GetData(Thread.GetNamedDataSlot("Random")).toString());

        // Allow time for other threads to show their data,
        // then demonstrate that any code a thread executes
        // has access to the thread's named data slot.        
        Thread.Sleep(1000);

        Other o = new Other();

        o.ShowSlotData();
    } //SlotTest
} //Slot

public class Other
{
    public void ShowSlotData()
    {
        // This method has no access to the data in the Slot
        // class, but when executed by a thread it can obtain
        // the thread's data from a named slot.
        Console.WriteLine("Other code displays data in "
            + "thread_{0}'s data slot: {1,3}", 
            String.valueOf(AppDomain.GetCurrentThreadId()),
            Thread.GetData(Thread.GetNamedDataSlot("Random")).ToString());
    } //ShowSlotData
} //Other
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

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

   

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



Thread.FreeNamedDataSlot メソッドのページの著作権

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

©2026 GRAS Group, Inc.RSS