Ejemplo n.º 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();
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 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);
            });
        }