Is there a way to do this (psedo code):
GetCurrentThread().Items.Add(new RefObject);
then later on retrive it
RefObject[] refObjs = GetCurrentThread().Items;
and enumerate the objects. Obviously both calls will occur on the same thread during the life span of it.
The basic idea is that I can easisly identify the items for the current thread were and that they are in fact created in this thread. It's same as using the Page.Request
object for an ASP.NET request - you know it's very unique in the worker process, so it doesn't get mixed up with other requests being served at the same time. I want to treat a thread like Page.Request
- it comes and then goes, but while it's alive, I can query it at will without caring about other threats that exist in the process.
Once I did something similar, but it was more of a simulation by marshaling objects by reference. And I can think of a couple of more ways to do it, but I really don't want to manually store the mapping between the thread and the object myself. Isn't there something similar to TransactionScope for threads in .NET? I am not too intersted in dealing with the hardship of syncronization and avoiding the race conditions.
PS: If that's not available, can I do it at least for an application domain without static variables?
There is the ThreadStatic attribute that you can put on a static field. From there, you can build whatever thread-specific data you desire. However, you won't be able to access the data from another thread (by thread ID for example).