views:

164

answers:

2

If I start a thread in the following manner

Thread newThread = new Thread(new ParameterizedThreadStart(MyThreadMethod));
Object myObject = new Object();
newThread.Start(myObject);

Can I find out what has it done to myObject after it has finished the task?

// at some point later
if(newThread.ThreadState == ThreadState.Stopped)
{
//access my object? how?
}
+2  A: 

You handed it the object. So just store the object that you hand alongside the thread that you start. Be very careful about what you do with it though, or you may run into interesting threading issues.

Noon Silk
So if I have a list of objects, and each object is processed in a separate thread, I should keep some flag or Id in each object, and then use the flag/Id to find my object in a list when it is processed?
Evgeny
I'd do ... `Dictionary<Thread, object> threadMap;` ...
Noon Silk
But of course ... this solution somehow escaped me ... thanks =)
Evgeny
+1  A: 

Sure. The stopping of the Thread in no way destroys the object passed off to it. As long as there is still a reference to the object, and it's not been disposed, it's still valid to use.

However there is no inherent way to get the value passed to the Thread::Start method back. Instead you'll have to keep a reference to it, probably from where you started the thread.

JaredPar