Freigeben über


Thread.FreeNamedDataSlot-Methode

Entfernt die Zuordnung zwischen einem Namen und einem Slot für alle Threads in dem Prozess.

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

Syntax

'Declaration
Public Shared Sub FreeNamedDataSlot ( _
    name As String _
)
'Usage
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
)

Parameter

  • name
    Der Name des freizugebenden Datenslots.

Hinweise

Hinweis

Das auf diese Methode angewendete HostProtectionAttribute-Attribut besitzt den Resources-Eigenschaftenwert SharedState | ExternalThreading. Das HostProtectionAttribute hat keine Auswirkungen auf Desktopanwendungen (die normalerweise durch Doppelklicken auf ein Symbol, Eingeben eines Befehls oder eines URL in einem Browser gestartet werden). Weitere Informationen finden Sie unter der HostProtectionAttribute-Klasse oder unter SQL Server-Programmierung und Hostschutzattribute.

Nach dem Aufrufen von FreeNamedDataSlot durch einen beliebigen Thread wird für jeden Thread, der GetNamedDataSlot mit demselben Namen aufruft, ein neuer, diesem Namen zugeordneter Slot reserviert. Nachfolgende Aufrufe von GetNamedDataSlot durch einen Thread geben den neuen Slot zurück. Jeder Thread, der über einen von einem früheren Aufruf von GetNamedDataSlot zurückgegebenen System.LocalDataStoreSlot verfügt, kann den alten Slot weiter verwenden..

Ein Slot, dem ein Name zugeordnet wurde, wird nur freigegeben, wenn jeder vor dem Aufruf von FreeNamedDataSlot erhaltene LocalDataStoreSlot freigegeben und die Garbage Collection ausgeführt wurde.

Threads verwenden zum Speichern threadspezifischer Daten einen Speichermechanismus mit lokalem Speicher. Die Common Language Runtime ordnet jedem Prozess bei der Generierung ein Datenspeicherarray mit mehreren Slots zu. Der Thread kann einen Datenslot im Datenspeicher reservieren, einen Datenwert im Slot speichern und aus diesem abrufen sowie den Slot für die weitere Verwendung nach Ablauf des Threads freigeben. Datenslots sind pro Thread jeweils eindeutig. Diese Daten kann kein anderer Thread (auch kein untergeordneter Thread) abrufen.

Beispiel

Im folgenden Codebeispiel wird die Verwendung eines benannten Datenslots zum Speichern von threadspezifischen Informationen veranschaulicht.

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

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Thread-Klasse
Thread-Member
System.Threading-Namespace

Weitere Ressourcen

Threads und Threading
Lokaler Threadspeicher und vom Thread verwendete statische Felder