示例#1
0
        public async Task SimpleEventHoldsWeakReference()
        {
            var eventFirer = new EventFirer();

            WeakReference reference;

            {
                var handler = new Handler <EventArgs>();
                reference = new WeakReference(handler);

                eventFirer.MySimpleEvent += handler.HandleEvent;
                eventFirer.OnMySimpleEvent();
            }

            var pos = 0;

            while (reference.Target != null && pos < 10)
            {
                await Task.Delay(1000);

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

                eventFirer.OnMyEvent("HelloWorld");

                pos++;
            }

            reference.Target.Should().BeNull();

            GC.KeepAlive(eventFirer);
        }
示例#2
0
        public void FiringSimpleEventFiresToHandler()
        {
            var fired = false;

            var eventFirer = new EventFirer();

            eventFirer.MySimpleEvent += (s, e) => { fired = true; };
            eventFirer.OnMySimpleEvent();

            fired.Should().BeTrue();
        }