Question

I have seen many questions on SO regarding the disposal of a BackgroundWorker in a Windows Forms application, and it appears that the consensus is that they do not need to be manually disposed in most situations. However, I have a question regarding how to handle a UserComponent that is being disposed when a BackgroundWorker is actually running. (IsBusy returns true) Should this situation be handled in any particular way?

For example I have a UserControl (panel-like object) that a user can enter settings information. It contains a BackgroundWorker in it that saves a user's configuration. When the panel looses focus (Leave event) it spins up the BackgroundWorker if it's not already running to save the configuration to file if a change is detected. Since the Leave event may also be the user changing to another screen this panel will now be disposed. If the BackgroundWorker is still running do I need to handle this in the Dispose method of the UserComponent itself?

Was it helpful?

Solution

It depends, we can't see your code. First off, it is pretty unlikely that you can implement CancelAsync() so don't bother with that. Spinning until IsBusy returns false in the Dispose() method is an option, but do beware of deadlock. You'll definitely get one when you implemented an event handler for RunWorkerCompleted. It is unlikely you have one so spinning ought to be okay.

The sane approach is to just let it run. The user will be happy. Some precautions are necessary:

  • The worker thread should still be able to access the properties of the control even if it was disposed. This is not normally a problem, you store the values of simple backing variables, they don't become invalid when the control is disposed. Easy to test btw, but if you are not sure then using a simple class that stores the values that need to be preserved and passing it to RunWorkerAsync() is always safe.

  • You need to deal with the risk of the worker thread being aborted because the program terminated. If that happens just as it is writing the file then the file will get corrupted. You solve this by writing to another file instead. And using File.Replace() to swap it. In general always a strategy you should use when you replace files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top