|
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
|
Tradução
Original
|
Método Thread.SetData
Namespace: System.Threading
Assembly: mscorlib (em mscorlib.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true, ExternalThreading = true)] public static void SetData( LocalDataStoreSlot slot, Object data )
Parâmetros
- slot
- Tipo: System.LocalDataStoreSlot
LocalDataStoreSlot para definir o valor.
- data
- Tipo: System.Object
O valor a ser definido.
Importante |
|---|
Observação |
|---|
Observação |
|---|
O atributo HostProtectionAttribute aplicado a este tipo ou membro tem o seguinte valor da propriedade Resources: SharedState | ExternalThreading. HostProtectionAttribute não afeta aplicativos de área de trabalho (que são normalmente iniciados com o clique duplo em um ícone, a digitação de um comando ou a inserção de uma URL em um navegador). Para obter mais informações, consulte a classe HostProtectionAttribute ou Atributos de proteção de Host e programação de SQL Server. |
primeiro exemplo
using System; using System.Threading; class Test { static void Main() { for(int i = 0; i < 3; i++) { Thread newThread = new Thread(ThreadData.ThreadStaticDemo); newThread.Start(); } } } class ThreadData { [ThreadStaticAttribute] 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 */
segundo exemplo
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)); newThreads[i].Start(); } Thread.Sleep(2000); for (i = 0; i < newThreads.Length; i++) { newThreads[i].Join(); Console.WriteLine("Thread_{0} finished.", newThreads[i].ManagedThreadId); } } } 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; Thread.SetData( Thread.GetNamedDataSlot("Random"), slotData); // 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. Thread.Sleep(1000); int newSlotData = (int)Thread.GetData(Thread.GetNamedDataSlot("Random")); if (newSlotData == slotData) { Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}", threadId, newSlotData); } else { Console.WriteLine("Data in thread_{0}'s data slot changed to: {1,3}", threadId, newSlotData); } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Função Server Core sem suporte), Windows Server 2008 R2 (Função Server Core com suporte com o SP1 ou posterior, Itanium sem suporte)
O .NET Framework não oferece suporte a todas as versões de cada plataforma. Para obter uma lista das versões com suporte, consulte .Requisitos de sistema do NET Framework.
Importante