Thread.SetData 方法
2008-04-09 14:51
330 查看
Thread.SetData 方法
在当前正在运行的线程上为此线程的当前域在指定槽中设置数据。
命名空间:System.Threading
程序集:mscorlib(在 mscorlib.dll 中)
语法
C#
C++
在其中设置值的 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(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();
}
}
在当前正在运行的线程上为此线程的当前域在指定槽中设置数据。
命名空间: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(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());
}
}
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( 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();
}
}
相关文章推荐
- Thread.SetData 方法
- (44)通过继承创建线程对象的例子+getName+setName+currentThread()方法应用介绍
- Thread的setDaemon(true)方法的作用
- Thread的setDaemon(true)方法的作用
- primesense_Warning: USB events thread - failed to set priority. This might cause loss of data...
- 找不到方法:“Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)”。
- DWR 对DATA的理解(调用返回List、Set或者Map的java方法)
- Thread-Safe的static data member初始化方法
- 找不到方法:“Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)
- 小程序this.setData is not a function;api request success callback function解决方法
- Thread setDaemon 方法
- 找不到方法:“Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)”。
- 【Java基础】Thread setDaemon 方法
- Html5之高级-11 拖放API (拖放事件、dataTransfer对象、setDragImage方法)
- Thread的setDaemon(true)方法的作用
- java.lang.Thread.setDefaultUncaughtExceptionHandler()方法实例
- LINQ entityset 不包含GetEnumerator的定义,在MVC中使用entity framework(EF)出现“必须添加对程序集“System.Data.Entity”解决方法
- Thread的setDaemon(true)方法的作用
- qt delegate代理 setModelData()方法
- EF修改实体状态(State)时出现异常找不到方法“System.Data.Entity.Infrastructure.set_State”