Proper way to Dispose of a BackGroundWorker

BackgroundWorker derives from Component. Component implements the IDisposable interface. That in turn makes BackgroundWorker inherit the Dispose() method.

Deriving from Component is a convenience for Windows Forms programmers, they can drop a BGW from the toolbox onto a form. Components in general are somewhat likely to have something to dispose. The Windows Forms designer takes care of this automatically, look in the Designer.cs file for a Form for the “components” field. Its auto-generated Dispose() method calls the Dispose() method for all components.

However, BackgroundWorker doesn’t actually have any member that requires disposing. It doesn’t override Dispose(). Its base implementation, Component.Dispose(), only makes sure that the component is removed from the “components” collection. And raise the Disposed event. But doesn’t otherwise dispose anything.

Long story short: if you dropped a BGW on a form then everything is taken care of automatically, you don’t have to help. If you didn’t drop it on a form then it isn’t an element in a components collection and nothing needs to be done.

You don’t have to call Dispose().

Leave a Comment