Export (0) Print
Expand All

Thread.GetData Method

Retrieves the value from the specified slot on the current thread, within the current thread's current domain.

[Visual Basic]
Public Shared Function GetData( _
   ByVal slot As LocalDataStoreSlot _
) As Object
[C#]
public static object GetData(
 LocalDataStoreSlot slot
);
[C++]
public: static Object* GetData(
 LocalDataStoreSlot* slot
);
[JScript]
public static function GetData(
   slot : LocalDataStoreSlot
) : Object;

Parameters

slot
The LocalDataStoreSlot from which to get the value.

Return Value

The retrieved value.

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.

[Visual Basic] Note    GetData is a Shared method that always applies to the currently executing thread, even if you call it using a variable that refers to another thread. To avoid confusion, use the class name when calling Shared methods: Dim test As Object = Thread.GetData(testSlot).

Example

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

[Visual Basic] 
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 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

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

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

__gc 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, 
            __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(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(S"Data in thread_{0}'s data slot: {1,3}", 
            AppDomain::GetCurrentThreadId().ToString(),
            Thread::GetData(localSlot)->ToString());
    }
};

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 | SetData | Threads and Threading | Thread Local Storage and Thread-Relative Static Fields

Show:
© 2014 Microsoft