Thread.AllocateDataSlot 方法
在所有的线程上分配未命名的数据槽。
**命名空间:**System.Threading
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Function AllocateDataSlot As LocalDataStoreSlot
用法
Dim returnValue As LocalDataStoreSlot
returnValue = Thread.AllocateDataSlot
public static LocalDataStoreSlot AllocateDataSlot ()
public:
static LocalDataStoreSlot^ AllocateDataSlot ()
public static LocalDataStoreSlot AllocateDataSlot ()
public static function AllocateDataSlot () : LocalDataStoreSlot
返回值
备注
提示
应用于此方法的 HostProtectionAttribute 属性 (attribute) 具有以下 Resources 属性 (property) 值:SharedState | ExternalThreading。HostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性。
该数据槽在所有线程上分配。
线程使用本地存储内存机制来存储线程特定的数据。公共语言运行库在创建每个进程时给它分配一个多槽数据存储区数组。线程可以在数据存储区中分配数据槽,在槽中存储和检索数据值,以及在线程过期后释放槽供重用。数据槽对于每个线程是唯一的。其他任何线程(即使是子线程)都无法获取这些数据。
示例
下面的代码示例阐释了如何使用数据槽存储线程特定的信息。
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 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
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());
}
}
using namespace System;
using namespace System::Threading;
ref class Slot
{
private:
static Random^ randomGenerator;
static LocalDataStoreSlot^ localSlot;
static Slot()
{
randomGenerator = gcnew 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() );
}
};
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;
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 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
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
Thread 类
Thread 成员
System.Threading 命名空间