|
using System;
using System.Collections.Generic;
using System.Threading;
using Pfz.Caching;
namespace Pfz
{
/// <summary>
/// Class responsible for calling dispose of its value, even if this class itself is not properly disposed.
/// </summary>
public class DisposeAssurerBase:
IAdvancedDisposable
{
internal static readonly object _lock = new object();
internal static HashSet<IDisposable> _hashset = new HashSet<IDisposable>(ReferenceComparer.Instance);
private static List<IDisposable> _toDispose = new List<IDisposable>();
private static AutoResetEvent _autoResetEvent = new AutoResetEvent(false);
static DisposeAssurerBase()
{
GCUtils.Collected += _Collected;
var domain = AppDomain.CurrentDomain;
domain.DomainUnload += _DisposeAll;
domain.ProcessExit += _DisposeAll;
Thread thread = new Thread(_Run);
thread.Name = "DisposeCaller";
thread.Start();
}
private static void _DisposeAll(object sender, EventArgs e)
{
GCUtils.Collected -= _Collected;
HashSet<IDisposable> hashset;
List<IDisposable> toDispose;
lock(_lock)
{
hashset = _hashset;
toDispose = _toDispose;
_hashset = null;
_toDispose = null;
}
_autoResetEvent.Set();
if (toDispose != null)
foreach(var disposable in toDispose)
disposable.Dispose();
if (hashset != null)
foreach(var disposable in hashset)
disposable.Dispose();
}
private static void _Collected()
{
lock(_lock)
{
var hashset = _hashset;
if (hashset == null)
{
GCUtils.Collected -= _Collected;
return;
}
hashset.TrimExcess();
}
}
private static void _Run()
{
var thread = Thread.CurrentThread;
while(true)
{
thread.IsBackground = true;
_autoResetEvent.WaitOne();
thread.IsBackground = false;
while(true)
{
List<IDisposable> toDispose;
lock(_lock)
{
toDispose = _toDispose;
if (toDispose == null)
return;
int count = toDispose.Count;
if (count == 0)
break;
_toDispose = new List<IDisposable>();
}
foreach(var disposable in toDispose)
disposable.Dispose();
}
}
}
internal DisposeAssurerBase()
{
}
/// <summary>
/// When inheriting this class, you should provide the value.
/// If the value can't be guaranteed to be disposed later (system is out of memory now)
/// it is disposed immediatelly and an exception is thrown.
/// </summary>
protected DisposeAssurerBase(IDisposable value)
{
if (value == null)
throw new ArgumentNullException("value");
_value = value;
try
{
lock(_lock)
_hashset.Add(value);
}
catch
{
GC.SuppressFinalize(this);
value.Dispose();
_value = null;
throw;
}
}
/// <summary>
/// Immediatelly releases all resources used by this object.
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
IDisposable value;
lock(_lock)
{
var hashset = _hashset;
if (hashset == null)
return;
value = _value;
if (value == null)
return;
_value = null;
if (!hashset.Remove(value))
return;
}
value.Dispose();
}
/// <summary>
/// Will try to dispose the referenced object.
/// </summary>
~DisposeAssurerBase()
{
// As threads can be stopped, we can't wait on lock. If we can't lock now, we need to retry later.
if (!Monitor.TryEnter(_lock))
{
GC.ReRegisterForFinalize(this);
return;
}
try
{
var value = _value;
var hashset = _hashset;
if (hashset == null)
return;
if (!hashset.Remove(value))
return;
try
{
_toDispose.Add(value);
}
catch
{
GC.ReRegisterForFinalize(this);
return;
}
if (_toDispose.Count == 1)
_autoResetEvent.Set();
}
finally
{
Monitor.Exit(_lock);
}
}
internal IDisposable _value;
/// <summary>
/// Gets the value of this DisposeCaller.
/// </summary>
protected IDisposable Value
{
get
{
var result = _value;
if (result == null)
throw new ObjectDisposedException(GetType().FullName);
return result;
}
}
/// <summary>
/// Gets a value indicating if this object was already disposed.
/// </summary>
public bool WasDisposed
{
get
{
return _value == null;
}
}
}
/// <summary>
/// Class responsible for calling dispose of its value, even if this class itself is not properly disposed.
/// </summary>
public class DisposeAssurerBase<T>:
DisposeAssurerBase
where
T: class, IDisposable
{
internal DisposeAssurerBase()
{
}
/// <summary>
/// When inheriting this class, you should provide the value.
/// If the value can't be guaranteed to be disposed later (system is out of memory now)
/// it is disposed immediatelly and an exception is thrown.
/// </summary>
protected DisposeAssurerBase(T value):
base(value)
{
}
/// <summary>
/// Gets the value of this DisposeCaller.
/// </summary>
protected new T Value
{
get
{
return (T)base.Value;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.
Want more info or simply want to contact me?
Take a look at:
http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).