I’m working on a WinForm project, using BackgroundWorker, for a data extraction procedure, and I periodically have to update a progressbar that shows the activity progress.
My question is: is there a way to decouple the extraction logic (in my case implemented in different classes) from the “progress report” logic?
You cannot decouple the logic of reporting the progress from the logic that does the work, but you can decouple the logic that reports progress from the logic that updates the progress bar.
One way of doing it is by providing an event in the class that does the work to report its progress to interested parties that registered an event handler with the logic that does the work, and call it periodically as the progress changes.
Your background worker would then become an “interested party” by registering to receive these event notifications from the “do the work” logic, and call its own
ReportProgresswith the data that it receives from the event. This way the “do the work” logic could remain unaware of theReportProgressof the background worker.