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 awlays 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();
}