Avatar of Mr Knackered
Mr Knackered
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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.
C#.NET Programming

Avatar of undefined
Last Comment
Jacques Bourgeois (James Burger)

8/22/2022 - Mon
Carl Tawn

In theory, no; but it doesn't hurt to safeguard it anyway just in case things change in the future.
Jacques Bourgeois (James Burger)

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 Knackered

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jacques Bourgeois (James Burger)

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