事件可能会引起一个内存泄露问题,而该模型就是为了解决该问题。
为什么会出现memory Leak
事件的使用语法如:source.Event+=Listener.Func
,这样Source即建立了一个Listener的强引用,从而Listener的生命周期将受到Source的影响,生命周期延长,从而产生内存泄露。
如何完成弱事件模型
- 从
WeakEventManager
派生出一个Manager。 - 在Listerner对象上实现
IWeakEventListener
接口。 - 当对Source添加Listener时,不要使用事件的Add或Remove操作,应该使用Manager中的
AddListener
及RemoveListener
。
自定义WeakEventManager
- 重写
StartListening
及StopListening
- 提供两个方法来操作Listeners List,常用命名为
Addlistener
及RemoveListener
- 实现一个
CurrentManager
属性
代码示例
class Source
{
public void Time()
{
var timeHandler = TimeChanged;
if (null != timeHandler)
timeHandler(this, new TimeEventArgs { TimeNow = DateTime.Now });
}
public event Action<object, TimeEventArgs> TimeChanged;
}
class TimeChangeEventManager : WeakEventManager
{
private static TimeChangeEventManager CurrentManager
{
get
{
var manager = (TimeChangeEventManager)GetCurrentManager(typeof(TimeChangeEventManager));
if (manager == null)
{
manager = new TimeChangeEventManager();
SetCurrentManager(typeof(TimeChangeEventManager), manager);
}
return manager;
}
}
public static void AddListener(Source source, IWeakEventListener listener)
{
CurrentManager.ProtectedAddListener(source, listener);
}
public static void RemoveListener(Source source, IWeakEventListener listener)
{
CurrentManager.ProtectedRemoveListener(source, listener);
}
protected override void StartListening(object source)
{
((Source)source).TimeChanged += DeliverEvent;
}
protected override void StopListening(object source)
{
((Source)source).TimeChanged -= DeliverEvent;
}
}
class ListenerTwo : IWeakEventListener
{
private static int S_ListenCount = 0;
private int _currentIndex = 0;
public ListenerTwo()
{
_currentIndex = S_ListenCount++;
}
public void Show(object sender, TimeEventArgs e)
{
Console.WriteLine("Index: {0} Time: {1}", _currentIndex, e.TimeNow);
}
public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
if (managerType == typeof (TimeChangeEventManager))
Show(sender, (TimeEventArgs) e);
else
{
return false;
}
return true;
}
}
class TimeEventArgs : EventArgs
{
public DateTime TimeNow { get; set; }
}
static void Main(string[] args)
{
Source ss = new Source();
AddListener(ss);
ss.Time();
Console.Read();
}
private static void AddListener(Source ss)
{
var lis1 = new ListenerTwo();
var lis2 = new ListenerTwo();
//可以释放
TimeChangeEventManager.AddListener(ss,lis1);
TimeChangeEventManager.AddListener(ss,lis2);
//不会释放
ss.TimeChanged += lis1.Show;
ss.TimeChanged += lis2.Show;
lis1 = null;
lis2 = null;
GC.Collect();
}
.Net 4.5的处理方式
4.5提供了WeakEventManager
的一个泛型版本,即WeakEventManager<TEventSource,TEventArgs>
。使用泛型版本则不需要自定义一个新的EventManager类。
用法相当的简单,如下代码所示:
WeakEventManager<Source, TimeEventArgs>.AddHandler(ss, "TimeChanged", lis1.Show);
引用
http://www.codeproject.com/Articles/738109/The-NET-weak-event-pattern-in-Csharp
https://msdn.microsoft.com/en-us/library/aa970850(v=vs.100).aspx
https://msdn.microsoft.com/en-us/library/system.windows.iweakeventlistener.receiveweakevent(v=vs.100).aspx
https://msdn.microsoft.com/en-us/library/system.windows.weakeventmanager(v=vs.100).aspx