I am using a BackgroundWorker a bit differently than I was used to.
Usually, I use the BW to update some graphical control, and I perform all the required task in the DoWork method, return the result and do the assignments to the RunWorkerCompleted event handler.
Here, I just wanted to assign the result of some computation (DB query) to some private variable _myList of the current Window.
I was very suprised to see that I was allowed to assign the list from the DoWork method, and I was really surprised.
I just wanted to know if it was normal or if it was not recommended for some reason?
You will be allowed to assign to the variable as it doesn’t have any checks guarding against cross-thread actions, the only things that do these checks are the UI elements.
As for if it is recommended, you are stepping into the realms of multi-threaded synchronisation.
Usually you lock access to a resource before attempting to use it, to guard against things like race conditions:
If your usage is basic, as in, you set into it then at the end of the background worker chunk you read from it, then you will be safe to do this. However, if other stuff attempts to write to it, or you have code that checks variable content before acting on it, then you start to get race conditions.