static void TestCollectingListener() { Console.WriteLine("TestCollectingListener"); // test that the { SmartEventSource source = new SmartEventSource(); EventListener r = new EventListener(source); r.Attach(); source.RaiseEvent(); GC.KeepAlive(r); r = null; GC.Collect(); GC.WaitForPendingFinalizers(); source.RaiseEvent(); } Console.WriteLine("With fast:"); { FastSmartEventSource source = new FastSmartEventSource(); EventListener r = new EventListener(source); r.Attach(); source.RaiseEvent(); GC.KeepAlive(r); r = null; GC.Collect(); GC.WaitForPendingFinalizers(); source.RaiseEvent(); } Console.WriteLine(); }
static void PerformanceTest() { SpeedTest( "Normal (strong) event", 5000000, callCount => { Program p = new Program(); NormalEventSource source = new NormalEventSource(); source.Event += StaticOnEvent; source.Event += p.OnEvent; for (int i = 0; i < callCount; i++) { source.RaiseEvent(); } GC.KeepAlive(p); }); SpeedTest( "Smart weak event", 200000, callCount => { Program p = new Program(); SmartEventSource source = new SmartEventSource(); source.Event += StaticOnEvent; source.Event += p.OnEvent; for (int i = 0; i < callCount; i++) { source.RaiseEvent(); } GC.KeepAlive(p); }); SpeedTest( "Fast smart weak event", 5000000, callCount => { Program p = new Program(); FastSmartEventSource source = new FastSmartEventSource(); source.Event += StaticOnEvent; source.Event += p.OnEvent; for (int i = 0; i < callCount; i++) { source.RaiseEvent(); } GC.KeepAlive(p); }); }
static void TestCollectingListener() { Console.WriteLine("TestCollectingListener"); Console.WriteLine("The event should be raised once, then the listener should get garbage collected."); { SmartEventSource source = new SmartEventSource(); EventListener r = new EventListener(source); r.Attach(); source.RaiseEvent(); GC.KeepAlive(r); r = null; GC.Collect(); GC.WaitForPendingFinalizers(); source.RaiseEvent(); } Console.WriteLine("With fast (2009 version):"); { FastSmartEventSource source = new FastSmartEventSource(); EventListener r = new EventListener(source); r.Attach(); source.RaiseEvent(); GC.KeepAlive(r); r = null; GC.Collect(); GC.WaitForPendingFinalizers(); source.RaiseEvent(); } Console.WriteLine("With fast (2013 version):"); { FastSmartEventSource2013 source = new FastSmartEventSource2013(); EventListener r = new EventListener(source); r.Attach(); source.RaiseEvent(); GC.KeepAlive(r); r = null; GC.Collect(); GC.WaitForPendingFinalizers(); source.RaiseEvent(); } Console.WriteLine(); }
public void Cleanup() { normalSource.Event -= StaticOnEvent; normalSource.Event -= OnEvent; normalSource = null; smartSource.Event -= StaticOnEvent; smartSource.Event -= OnEvent; smartSource = null; fastSmartSource.Event -= StaticOnEvent; fastSmartSource.Event -= OnEvent; fastSmartSource = null; fastSmartSource2013.Event -= StaticOnEvent; fastSmartSource2013.Event -= OnEvent; fastSmartSource2013 = null; thomasSource.Unsubscribe(StaticOnEvent); thomasSource.Unsubscribe(OnEvent); thomasSource = null; }
public void Setup() { normalSource = new NormalEventSource(); normalSource.Event += StaticOnEvent; normalSource.Event += OnEvent; smartSource = new SmartEventSource(); smartSource.Event += StaticOnEvent; smartSource.Event += OnEvent; fastSmartSource = new FastSmartEventSource(); fastSmartSource.Event += StaticOnEvent; fastSmartSource.Event += OnEvent; fastSmartSource2013 = new FastSmartEventSource2013(); fastSmartSource2013.Event += StaticOnEvent; fastSmartSource2013.Event += OnEvent; thomasSource = new WeakEvent.WeakEventSource <EventArgs>(); thomasSource.Subscribe(StaticOnEvent); thomasSource.Subscribe(OnEvent); }