Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Created July 3, 2017 10:33
Show Gist options
  • Save Pzixel/d238c51a24f91ffc15fdfef7ffe4a4be to your computer and use it in GitHub Desktop.
Save Pzixel/d238c51a24f91ffc15fdfef7ffe4a4be to your computer and use it in GitHub Desktop.
Base disposable class that incapsulate thread-safe dispose logic
using System;
using System.Threading;
/// <summary>
/// DisposableBase class. Represents an implementation of the IDisposable interface.
/// </summary>
public abstract class DisposableBase : IDisposable
{
/// <summary>
/// A value which indicates the disposable state. 0 indicates undisposed, 1 indicates disposing
/// or disposed.
/// </summary>
private volatile int _disposableState;
protected bool IsDisposed => _disposableState == 1;
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with disposing of resources.
/// </summary>
public void Dispose()
{
#pragma warning disable 420
if (Interlocked.CompareExchange(ref _disposableState, 1, 0) == 0)
#pragma warning restore 420
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
#endregion IDisposable Members
/// <summary>
/// Dispose resources. Override this method in derived classes. Unmanaged resources should always be released
/// when this method is called. Managed resources may only be disposed of if disposing is true.
/// </summary>
/// <param name="disposing">A value which indicates whether managed resources may be disposed of.</param>
protected abstract void Dispose(bool disposing);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment