Beispiel #1
0
        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();
        }
Beispiel #2
0
 static void TestAttachAnonymousMethod()
 {
     Console.WriteLine("TestAttachAnonymousMethod");
     try {
         FastSmartEventSource source = new FastSmartEventSource();
         string text = "Hi";
         source.Event += delegate {
             Console.WriteLine(text);
         };
         Console.WriteLine("Attaching an anonymous method that captures local variables should result in an exception!");
     } catch (Exception ex) {
         Console.WriteLine(ex.Message);
     }
     Console.WriteLine();
 }
Beispiel #3
0
        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);
            });
        }
Beispiel #4
0
        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();
        }
Beispiel #5
0
        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;
        }
Beispiel #6
0
        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);
        }