Use thread-local storage
using System;
using System.Threading;
class MainClass
{
public static void SetError()
{
Random r = new Random();
Thread.SetData(Thread.GetNamedDataSlot("Number"), r.Next(100));
Thread.SetData(Thread.GetNamedDataSlot("Name") ,Thread.CurrentThread.Name);
Console.WriteLine("Number = " + Thread.GetData(Thread.GetNamedDataSlot("Number")));
Console.WriteLine("Name = " + Thread.GetData(Thread.GetNamedDataSlot("Name")));
}
public static void Main()
{
Thread.AllocateNamedDataSlot("Number");
Thread.AllocateNamedDataSlot("Name");
Thread t2 = new Thread(new ThreadStart(SetError));
t2.Name = "t2";
t2.Start();
Thread t3 = new Thread(new ThreadStart(SetError));
t3.Name = "t3";
t3.Start();
Thread.FreeNamedDataSlot("Number");
Thread.FreeNamedDataSlot("Name");
}
}
Output Number = 81
Name = t2
Number = 81
Name = t3
|
HTML code for linking to this page:
Related in same category :
-
-
-
-
|