?
Solved

IS ThreadPool.QueueUserWorkItem safe to insert parameters?

Posted on 2011-09-06
2
Medium Priority
?
344 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:JSW21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
Gewgala earned 1500 total points
ID: 36487376
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.
0
 

Author Closing Comment

by:JSW21
ID: 36490385
Hope this is right, since thread 1 will never reuse/switch the client anywhere.
Thread 1 always created anew.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question