Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Thread.GetNamedDataSlot Method

Looks up a named data slot.

[Visual Basic]
Public Shared Function GetNamedDataSlot( _
   ByVal name As String _
) As LocalDataStoreSlot
[C#]
public static LocalDataStoreSlot GetNamedDataSlot(
 string name
);
[C++]
public: static LocalDataStoreSlot* GetNamedDataSlot(
 String* name
);
[JScript]
public static function GetNamedDataSlot(
   name : String
) : LocalDataStoreSlot;

Parameters

name
The name of the local data slot.

Return Value

A LocalDataStoreSlot allocated for this thread.

Remarks

Threads use a local store memory mechanism to store thread-specific data. The common language runtime allocates a multi-slot data store array to each process when it is created. The thread can allocate a data slot in the data store, store and retrieve a data value in the slot, and free the slot for reuse after the thread expires. Data slots are unique per thread. No other thread (not even a child thread) can get that data.

If the named slot does not exist, a new slot is allocated. Named data slots are public and can be manipulated by anyone.

Example

[Visual Basic, C#, C++] The following code example demonstrates how to use a named data slot to store thread-specific information.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.Threading

Class Test

    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

[C#] 
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());
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc 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}", 
            __box(AppDomain::GetCurrentThreadId()), 
            Thread::GetData( 
            Thread::GetNamedDataSlot("Random"))->ToString());
    }
};

__gc class Slot
{
    static Random* randomGenerator = new Random();

public:
    static void SlotTest()
    {
        // Set different data in each thread's data slot.
        Thread::SetData(
            Thread::GetNamedDataSlot(S"Random"), 
            __box(randomGenerator->Next(1, 200)));

        // Write the data from each thread's data slot.
        Console::WriteLine(S"Data in thread_{0}'s data slot: {1,3}", 
            AppDomain::GetCurrentThreadId().ToString(),
            Thread::GetData(
            Thread::GetNamedDataSlot(S"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(S"Data in thread_{0}'s data slot is still: {1,3}", 
            AppDomain::GetCurrentThreadId().ToString(),
            Thread::GetData(
            Thread::GetNamedDataSlot(S"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();
    }
};

void main()
{
    Thread* newThreads __gc[] = new Thread* __gc[4];
    for(int i = 0; i < newThreads->Length; i++)
    {
        newThreads[i] = new Thread(
            new ThreadStart(0, &Slot::SlotTest));
        newThreads[i]->Start();
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Thread Class | Thread Members | System.Threading Namespace | Threads and Threading | Thread Local Storage and Thread-Relative Static Fields

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft