IS ThreadPool.QueueUserWorkItem safe to insert parameters?

JSW21
JSW21 used Ask the Experts™
on
client = vary;
data = vary;

ThreadPool.QueueUserWorkItem(
                            delegate
                                {
                                    SendData(Client, data);
                                });

Will my SendData get the correct Client, and data in multi thread application?
My goal is that Client is will be the right guy, during ThreadPool.QueueUserWorkItem called no changing.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
That should work fine, but one thing you might want to understand is that you're passing the Client object and Data in by value if they are an initiation of a class rather than a raw data type like int or double etc.

This means if you initiate the object on Thread 1 and pass it to Thread 2, Thread 1 will continue execution at the same time Thread 2 does.  If Thread 2 modifies a property on it, the same property is modified for Thread 1 and it won't necessarily happen at the exact same time every time.  You could end up with race conditions if execution is to continue and use the same object on Thread 1 and Thread 2.

I would either pass in a clone of the object to Thread 2 OR cease using the object in Thread 1 until Thread 2 has completed its operation entirely.

Author

Commented:
Hope this is right, since thread 1 will never reuse/switch the client anywhere.
Thread 1 always created anew.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial