public virtual void Dispose()
{
if (Interlocked.CompareExchange(ref status, 0, 0) == Status.Disposed)
return;
Stop();
if (Interlocked.CompareExchange(ref status, Status.Disposed, Status.Stopped) != Status.Stopped)
Interlocked.CompareExchange(ref status, Status.Disposed, Status.Stopping);
try
{
if (OnDispose != null)
OnDispose();
}
catch (Exception ex)
{
Log.Error("Error OnDispose(): ", ex);
}
try
{
Thread.Sleep(100); //give it a small chance to die gracefully
KillBgThreadIfExists();
}
catch (Exception ex)
{
if (this.OnError != null) this.OnError(ex);
}
DisposeHeartbeatTimer();
}
}