Thread.SetData 方法

148 篇文章 1 订阅
Thread.SetData 方法

在当前正在运行的线程上为此线程的当前域在指定槽中设置数据。

命名空间:System.Threading
程序集:mscorlib(在 mscorlib.dll 中)

C#
public static void SetData (
    LocalDataStoreSlot slot,
    Object data
)
C++
public:
static void SetData (
    LocalDataStoreSlot^ slot, 
    Object^ data
)
参数
slot

在其中设置值的 LocalDataStoreSlot

data

要设置的值。

C#

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

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

}
 
C++
     
     
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( 1200 ) );
      
      
// 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();

   }

}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值