Solved

IS ThreadPool.QueueUserWorkItem safe to insert parameters?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 44
How to join on ID, with prefix? 15 58
InputLanguage 1 25
get row value in vb.net 4 14
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

816 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now