|
using System;
using System.Collections.Generic;
using System.Linq;
using Pfz.Extensions.MonitorLockExtensions;
using Pfz.Threading;
namespace Pfz.Caching
{
/// <summary>
/// This class acts as a hashset for delegates, but allows the
/// Targets to be collected, working as if the Delegates where
/// WeakDelegates.
/// The original idea was to make this a generic class, but it
/// is not possible to use Delegate as a constraint (where T: Delegate).
///
/// To use, implement your event like:
/// private WeakDelegateSet fMyEvent = new WeakDelegateSet();
/// public event EventHandler MyEvent
/// {
/// add
/// {
/// fMyEvent.Add(value);
/// }
/// remove
/// {
/// fMyEvent.Remove(value);
/// }
///
/// And when you want to invoke MyEvent, you call:
/// fMyEvent.Invoke(this, EventArgs.Empty);
/// }
/// </summary>
public class WeakDelegateSet:
ThreadSafeDisposable
{
#region Private dictionary
private volatile Dictionary<int, List<WeakDelegateBase>> fDictionary = new Dictionary<int, List<WeakDelegateBase>>();
#endregion
#region Constructor
/// <summary>
/// Creates a new WeakDelegateSet.
/// </summary>
public WeakDelegateSet()
{
GCUtils.Collected += p_Collected;
}
#endregion
#region Dispose
/// <summary>
/// Unregisters the WeakDelegateSet from GCUtils.Collected.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing)
GCUtils.Collected -= p_Collected;
base.Dispose(disposing);
}
#endregion
#region p_Collected
private void p_Collected()
{
try
{
AbortSafe.Lock
(
DisposeLock,
delegate
{
if (WasDisposed)
{
GCUtils.Collected -= p_Collected;
return;
}
var oldDictionary = fDictionary;
var newDictionary = new Dictionary<int, List<WeakDelegateBase>>();
foreach(var pair in oldDictionary)
{
List<WeakDelegateBase> oldList = pair.Value;
List<WeakDelegateBase> newList = new List<WeakDelegateBase>(oldList.Count);
foreach(WeakDelegateBase handler in oldList)
if (handler.Method.IsStatic || handler.Target != null)
newList.Add(handler);
if (newList.Count > 0)
newDictionary.Add(pair.Key, newList);
}
fDictionary = newDictionary;
}
);
}
catch
{
}
}
#endregion
#region Clear
/// <summary>
/// Clears the delegate set.
/// </summary>
public void Clear()
{
DisposeLock.LockWithTimeout
(
delegate
{
CheckUndisposed();
fDictionary.Clear();
}
);
}
#endregion
#region Add
/// <summary>
/// Adds a new handler to the delegate set.
/// </summary>
/// <param name="handler">The handler to add.</param>
/// <returns>true if the delegate was new to the set, false otherwise.</returns>
public bool Add(Delegate handler)
{
if (handler == null)
throw new ArgumentNullException("handler");
int hashCode = handler.GetHashCode();
bool result = false;
DisposeLock.LockWithTimeout
(
delegate
{
CheckUndisposed();
var dictionary = fDictionary;
List<WeakDelegateBase> list;
if (dictionary.TryGetValue(hashCode, out list))
{
foreach(WeakDelegateBase action in list)
if (action.Target == handler.Target && action.Method == handler.Method)
return;
}
else
{
list = new List<WeakDelegateBase>(1);
dictionary.Add(hashCode, list);
}
WeakDelegateBase weakDelegate = new WeakDelegateBase(handler);
list.Add(weakDelegate);
result = true;
}
);
return result;
}
#endregion
#region Remove
/// <summary>
/// Removes a handler from the delegate set.
/// </summary>
/// <param name="handler">The handler to remove.</param>
/// <returns>true if the item was in the set, false otherwise.</returns>
public bool Remove(Delegate handler)
{
if (handler == null)
throw new ArgumentNullException("handler");
int hashCode = handler.GetHashCode();
bool result = false;
DisposeLock.LockWithTimeout
(
delegate
{
CheckUndisposed();
var dictionary = fDictionary;
List<WeakDelegateBase> list;
if (!dictionary.TryGetValue(hashCode, out list))
return;
int count = list.Count;
for(int i=0; i<count; i++)
{
WeakDelegateBase weakDelegate = list[i];
if (weakDelegate.Method == handler.Method && weakDelegate.Target == handler.Target)
{
list.RemoveAt(i);
result = true;
return;
}
}
}
);
return result;
}
#endregion
#region Invoke
/// <summary>
/// Invokes all the handlers in the set with the given parameters.
/// </summary>
/// <param name="parameters">The parameters to be used in the invoke of each handler.</param>
public void Invoke(params object[] parameters)
{
List<List<WeakDelegateBase>> copy = null;
DisposeLock.LockWithTimeout
(
delegate
{
CheckUndisposed();
copy = new List<List<WeakDelegateBase>>(fDictionary.Values.ToList());
}
);
foreach(var list in copy)
{
foreach(var handler in list)
{
object target = handler.Target;
if (target != null || handler.Method.IsStatic)
handler.Method.Invoke(target, parameters);
}
}
}
#endregion
}
}
|
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).