Good Morning all,
I wonder if I might trouble some experienced C# programmers for a piece of methodology advice. Here's my scenario:
I have created a bunch of classes each of which is required by an abstract base class to have a run() method. The run method executes a specific set of tasks and is different for each derived class.
In my main form I have created instances of these classes and grouped them together in a collection. I then iterate through the collection, calling the run() method on each object. This process is done via a background worker.
And here's where my programming methodology query starts. Each of the run() methods generates data, some of which I log to an SQL database and some of which I also put into text boxes on my main form.
When I started with this whole idea my intention was for the collection of objects to raise events when they had generated their data, and this data would be carried along in the event's argument. I would then subscribe to these events in my main form and only update the text boxes that were relevant to the data that had been collected for the object concerned.
However, I experienced problems with this idea because the background worker complained about me trying to update text boxes from a different thread. I was hoping that event driven programming was going to avoid this problem but it didn't.
So what I'm doing now instead is I have a static class that contains some public properties. Each object updates the properties relevant to its data. Each time an object's run method has been executed my background worker raises a progress changed event and then in my main form I update ALL of the text boxes with ALL of the data in my static class. This achieves my goal but is clearly more wasteful because I have to update the entire form each time an object has finished its run() method rather than just updating specific parts of the form for each object.
So I am wondering if I have turned a decent original concept into a bit of a dog's dinner. Perhaps a more experienced C# programmer could set me on the path to a much better way of solving this problem.
Any advice is most welcome.
Thanks,
Brian.
additional information copied from comment below
To clarify, there is one background worker thread which runs a loop. The loop iterates through my collection of objects calling the run() method on each one. Each run() method generates some data, and then on each iteration of the loop I am using the background worker's progress changed event to update all of my text boxes. But really what I wanted was for each object to raise its own event that I could subscribe to on my main form and only update the text boxes that are relevant to each objects data.