Search code examples
c#multithreadingparameterized

If I pass an object to a thread via ParameterizedThreadStart, can I access it later?


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?
}

Solution

  • 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.