Exemplo n.º 1
0
        public void SingleEventHandler()
        {
            var  eventSource  = new MockEventSource();
            var  eventHandler = new MockEventHandler();
            var  eventArgs    = new EventArgs();
            bool eventRaised  = false;

            eventHandler.Action = (s, e) =>
            {
                Assert.AreSame(eventSource, s);
                Assert.AreSame(eventArgs, e);
                eventRaised = true;
            };

            eventSource.MyEvent += eventHandler.OnEvent;
            eventSource.RaiseMyEvent(eventArgs);
            Assert.IsTrue(eventRaised);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            eventRaised = false;
            eventSource.RaiseMyEvent(eventArgs);
            Assert.IsTrue(eventRaised);

            eventRaised          = false;
            eventSource.MyEvent -= eventHandler.OnEvent;
            eventSource.RaiseMyEvent(eventArgs);
            Assert.IsFalse(eventRaised);
        }
Exemplo n.º 2
0
        public void RegisterAndUnregisterEventHandler()
        {
            var  eventSource  = new MockEventSource();
            var  eventHandler = new MockEventHandler();
            var  eventArgs    = new EventArgs();
            bool eventRaised  = false;

            eventHandler.Action = (s, e) =>
            {
                Assert.AreSame(eventSource, s);
                Assert.AreSame(eventArgs, e);
                eventRaised = true;
            };

            eventSource.MyEvent += eventHandler.OnEvent;
            eventSource.MyEvent -= eventHandler.OnEvent;
            eventSource.RaiseMyEvent(eventArgs);
            Assert.IsFalse(eventRaised);
        }