Windows apps
Collapse the table of content
Expand the table of content
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.SetData Method (LocalDataStoreSlot, Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Sets the data in the specified slot on the currently running thread, for that thread's current domain. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true, 
	ExternalThreading = true)]
public static void SetData(
	LocalDataStoreSlot slot,
	object data


Type: System.LocalDataStoreSlot

The LocalDataStoreSlot in which to set the value.

Type: System.Object

The value to be set.


The .NET Framework provides two mechanisms for using thread local storage (TLS): thread-relative static fields (that is, fields that are marked with the ThreadStaticAttribute attribute) and data slots. Thread-relative static fields provide much better performance than data slots, and enable compile-time type checking. For more information about using TLS, see Thread Local Storage: Thread-Relative Static Fields and Data Slots.

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 procedure ends and the Thread object has been reclaimed by garbage collection. Data slots are unique per thread. No other thread (not even a child thread) can get that data.


SetData 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: Thread.SetData(testSlot, "test data").

This section contains two code examples. The first example shows how to use a field that is marked with the ThreadStaticAttribute attribute to hold thread-specific information. The second example shows how to use a data slot to do the same thing.

First Example

The following example shows how to use a field that is marked with ThreadStaticAttribute to hold thread-specific information. This technique provides better performance than the technique that is shown in the second example.

using System;
using System.Threading;

class Test
    static void Main()
        for(int i = 0; i < 3; i++)
            Thread newThread = new Thread(ThreadData.ThreadStaticDemo);

class ThreadData
    static int threadSpecificData;

    public static void ThreadStaticDemo()
        // Store the managed thread id for each thread in the static
        // variable.
        threadSpecificData = Thread.CurrentThread.ManagedThreadId;

        // Allow other threads time to execute the same code, to show
        // that the static data is unique to each thread.
        Thread.Sleep( 1000 );

        // Display the static data.
        Console.WriteLine( "Data for managed thread {0}: {1}", 
            Thread.CurrentThread.ManagedThreadId, threadSpecificData );

/* This code example produces output similar to the following:

Data for managed thread 4: 4
Data for managed thread 5: 5
Data for managed thread 3: 3

Second Example

The following example demonstrates how to use a named data slot to store thread-specific information.

using System;
using System.Threading;

class Test
    public static void Main()
        Thread[] newThreads = new Thread[4];
        int i;
        for (i = 0; i < newThreads.Length; i++)
            newThreads[i] =
                new Thread(new ThreadStart(Slot.SlotTest));
        for (i = 0; i < newThreads.Length; i++)
            Console.WriteLine("Thread_{0} finished.",

class Slot
    private static Random randomGenerator = new Random();

    public static void SlotTest()
        // Set random data in each thread's data slot.
        int slotData = randomGenerator.Next(1, 200);
        int threadId = Thread.CurrentThread.ManagedThreadId;


        // Show what was saved in the thread's data slot.
        Console.WriteLine("Data stored in thread_{0}'s data slot: {1,3}",
            threadId, slotData);

        // Allow other threads time to execute SetData to show
        // that a thread's data slot is unique to itself.

        int newSlotData =

        if (newSlotData == slotData)
            Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}",
                threadId, newSlotData);
            Console.WriteLine("Data in thread_{0}'s data slot changed to: {1,3}",
                threadId, newSlotData);

.NET Framework
Available since 1.1
Return to top
© 2018 Microsoft