Solved

IS ThreadPool.QueueUserWorkItem safe to insert parameters?

Posted on 2011-09-06
2
338 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Service FileSystemwatcher isse 9 46
Converting PHP triple des decrypt to c# 9 75
"lblTime is not declared" 3 35
CHANGE FORM TEXT BEFORE IT SHOW TO USER 13 19
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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