C# Background Workers

Just a quick question re background workers:

I have multiple background workers that all run at the same time and they all need to read values from an observable collection. The observable collection contains data that is populated prior to the background workers being run and the data in it will never change and is never written to by any of the background workers. It contains static data the background workers need to calculate stuff.

Is it thread safe to allow the background workers in their DoWork events to access the local variable in the class that defines and runs the background workers which contains the collection as a property?

I can pass the collection into the background workers via e.Parameter (by adding a property to the object that is being passed in along with other parameters to the dowork event) but is this necessary as no writing is being performed on the collection at any point.
LVL 2
Mr KnackeredAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
In theory, no; but it doesn't hurt to safeguard it anyway just in case things change in the future.
Jacques Bourgeois (James Burger)PresidentCommented:
A note about terminology. It's important to express yourself with the right words.

A local variable is a variable declared in a method. It is visible only inside of the method. The only way to access it from another method is to pass it as a parameter.

A variable defined at the Form level is called a member. There is no such thing as a local variable in the class. Local variables are in a method.

And I am not sure your question is formulated correctly. The way it read it (maybe my French background makes me miss something), you want to read the local variable in the class that defines and runs the background workers from inside the BackgroundWorker DoWork method. I have never seen a situation when a BackgroundWorker needs to reference itself.

And what does the background workers which contains the collection as a property means? None of the properties of a BackgroundWorker is a collection. So, how can the BW contain a collection as a property. Once again, maybe I am misled, but from my point of view, I think that your question should be rephrased.
Mr KnackeredAuthor Commented:
Hi Jacques and thanks for your comment.

This is WPF and C# not Windows Forms but that shouldn't matter.

There is a controller class that has public properties that contain things such as the progress of each BGW and these are bound to in WPF to display progress. There are also local (private) variables in the controller class that contain static data and some of these are Observable Collections. The controller runs a process that retrieves all of the static data required for the multiple background workers and puts them into the local properties. Then the controller runs the BGW's and from within the DoWork events the local properties are read to access the static data required.

I have changed the controller initialisation process to form a parameter object that passes these parameters (including OC's) into the BGW threads as it seems to make more sense to do this and its cleaner when understanding what is going on. It works perfectly well under both scenarios  as long as the OC's are only being read from.
käµfm³d 👽Commented:
@James Burger
A variable defined at the Form level is called a member.
Any variable, property, or method belonging to a class is called a "member". A variable defined at class level is called a field.

@Mr Knackered
Without locking or some other thread synchronization, or without using one of the concurrent collections, your logic is not thread safe.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
You're right Kaufmed. I so rarely use Public fields that I forgot about them.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.