C# Background Workers

Mr Knackered
Mr Knackered used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Carl TawnSenior Systems and Integration Developer

Commented:
In theory, no; but it doesn't hurt to safeguard it anyway just in case things change in the future.
Top Expert 2015

Commented:
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.

Author

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.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
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.
Top Expert 2015

Commented:
You're right Kaufmed. I so rarely use Public fields that I forgot about them.

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