Exemplo n.º 1
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();
        }
Exemplo n.º 2
0
 static void TestAttachAnonymousMethod2013()
 {
     Console.WriteLine("TestAttachAnonymousMethod2013");
     try
     {
         FastSmartEventSource2013 source = new FastSmartEventSource2013();
         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("Got expected exception: " + ex.Message);
     }
     Console.WriteLine();
 }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
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);
        }