Esporta (0) Stampa
Espandi tutto
EN
Il contenuto non è disponibile nella lingua di destinazione, ma solo in inglese.

Thread.GetData Method

Retrieves the value from the specified slot on the current thread, within the current thread's current domain. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.

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

public static Object GetData (
	LocalDataStoreSlot slot
)
public static Object GetData (
	LocalDataStoreSlot slot
)
public static function GetData (
	slot : LocalDataStoreSlot
) : Object
Not applicable.

Parameters

slot

The LocalDataStoreSlot from which to get the value.

Return Value

The retrieved value.

NoteImportant:

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 and Thread-Relative Static Fields.

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.

NoteNote:

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).

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.

Second Example

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

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

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Aggiunte alla community

AGGIUNGI
Microsoft sta conducendo un sondaggio in linea per comprendere l'opinione degli utenti in merito al sito Web di MSDN. Se si sceglie di partecipare, quando si lascia il sito Web di MSDN verrà visualizzato il sondaggio in linea.

Si desidera partecipare?
Mostra:
© 2014 Microsoft