Since BackgroundWorker
implements IDisposable
, you should be disposing it when you are finished with it.
You mention that this is in a Windows service so there are a few things that come into play here. When you stop a Windows service, you get around 30 seconds to return from the OnStop
method in your ServiceBase
implementation. If you do not return within this time, Windows reports to the user that it could not stop the service. What you should do is signal to the BackgroundWorker
that you need it to stop (using the CancelAsync
mechanism), dispose of the worker, and then exit. However, since you are stopping the service, it doesn't really matter as the whole process will be terminated anyway, including any threads that are running within it.
If you were to do as you say and wait for the worker to complete (in the OnStop
method), your service may appear to the user as if it has hung as Windows will say that it cannot stop it, and the process will still be running.