I've got a class named BackgroundWorker
that has a thread constantly running. To turn this thread off, an instance variable named stop
to needs to be true
.
To make sure the thread is freed when the class is done being used, I've added IDisposable
and a finalizer that invokes Dispose()
. Assuming that stop = true
does indeed cause this thread to exit, is this sippet correct? It's fine to invoke Dispose
from a finalizer, right?
Finalizers should always call Dispose
if the object
inherits IDisposable
, right?
/// <summary>
/// Force the background thread to exit.
/// </summary>
public void Dispose()
{
lock (this.locker)
{
this.stop = true;
}
}
~BackgroundWorker()
{
this.Dispose();
}
First off, a severe warning. Don't use a finalizer like you are. You are setting yourself up for some very bad effects if you take locks within a finalizer. Short story is don't do it. Now to the original question.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Force the background thread to exit.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
lock (this.locker)
{
this.stop = true;
}
}
}
~BackgroundWorker()
{
Dispose(false);
}
The only reason to have a finalizer at all is to allow sub-classes to extend and release unmanaged resources. If you don't have subclasses then seal your class and drop the finalizer completely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With