c#弱事件(weak event)
传统事件publisher和listener是直接相连的,这样会对垃圾回收带来一些问题,例如listener已经不引用任何对象但它仍然被publisher引用
垃圾回收器就不能回收listener所占用的内存以及publisher仍然引用该listener向其触发事件。
而弱事件就解决了这个问题
WeakEventManager是publisher和listener之间的桥梁,从而取代两者之间的直接连接
一、定义WeakEventManager类
public class WeakCarInfoEventManager : WeakEventManager
{
public static void AddListener(object source,IWeakEventListener listener)
{
CurrentManager.ProtectedAddListener(source, listener);
}
public static void RemoveListener(object source,IWeakEventListener listener)
{
CurrentManager.ProtectedRemoveListener(source, listener);
}
public static WeakCarInfoEventManager CurrentManager
{
get
{
var manager = GetCurrentManager(typeof(WeakCarInfoEventManager))
as WeakCarInfoEventManager;
if(manager == null)
{
manager = new WeakCarInfoEventManager();
SetCurrentManager(typeof(WeakCarInfoEventManager), manager);
}
return manager;
}
}
protected override void StartListening(object source)
{
(source as CarDealer).NewCarInfo += CarDealer_NewCarInfo;
}
void CarDealer_NewCarInfo(object sender,CarInfoEventArgs e)
{
DeliverEvent(sender, e);
}
protected override void StopListening(object source)
{
(source as CarDealer).NewCarInfo -= CarDealer_NewCarInfo;
}
}
二、定义事件监听者(listener)
public class Consumer:IWeakEventListener //主要要继承改接口
{
private string name;
public Consumer(string name)
{
this.name = name;
}
public void NewCarIsHere(object sender,CarInfoEventArgs e)
{
Console.WriteLine("{0}:car {1} is new", name, e.Car);
}
bool IWeakEventListener.ReceiveWeakEvent(Type managerType,object sender,EventArgs e)
{
NewCarIsHere(sender, e as CarInfoEventArgs);
return true;
}
}
三、事件发布者(publisher)
public class CarInfoEventArgs : EventArgs //自定义事件数据参数
{
public CarInfoEventArgs(string car)
{
this.Car = car;
}
public string Car { get; private set; }
}
public class CarDealer
{
public event EventHandler<CarInfoEventArgs> NewCarInfo;
public void NewCar(string car)
{
Console.WriteLine("Car dealer, new car {0}", car);
RaiseNewCarInfo(car);
}
protected virtual void RaiseNewCarInfo(string car)
{
EventHandler<CarInfoEventArgs> newCarInfo = NewCarInfo;
if (newCarInfo != null)
{
newCarInfo(this, new CarInfoEventArgs(car));
}
}
}
四、在启动函数中通过WeakCarInfoEventManager把publisher和listener联系起来
class Program
{
static void Main(string[] args)
{
var dealer = new CarDealer();
var tian = new Consumer("tian");
WeakCarInfoEventManager.AddListener(dealer, tian);
dealer.NewCar("奔驰");
var tian1 = new Consumer("tian1");
WeakCarInfoEventManager.AddListener(dealer, tian1);
dealer.NewCar("东风");
Console.ReadKey();
}
}
运行结果如下:
最后.net4.5版本已经将weakeventmanager集成进来这样使用会更加方便
static void Main(string[] args)
{
var dealer = new CarDealer();
var tian = new Consumer("tian");
//WeakCarInfoEventManager.AddListener(dealer, tian);
WeakEventManager<CarDealer, CarInfoEventArgs>.AddHandler(dealer, "NewCarInfo", tian.NewCarIsHere);
dealer.NewCar("奔驰");
var tian1 = new Consumer("tian1");
//WeakCarInfoEventManager.AddListener(dealer, tian1);
WeakEventManager<CarDealer, CarInfoEventArgs>.AddHandler(dealer, "NewCarInfo", tian1.NewCarIsHere);
dealer.NewCar("东风");
Console.ReadKey();
}
与上面的运行结果是一样的