コード例 #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);
        }
コード例 #2
0
        public void EventWithInternalEventHandler()
        {
            var  eventSource  = new MockEventSource();
            var  eventHandler = new MockInternalEventHandler();
            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);
        }
コード例 #3
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);
        }