コード例 #1
0
        public void ImplicitRelease()
        {
            var supplier = new OnDemandEvent <int>();
            int reciever = 0;
            WeakReference <IDisposable> weak = null;

            void exec()
            {
                var d = supplier.WeakSubscribe <OnDemandEvent <int> >(
                    (sup, hdr) => sup.TestEvent += hdr,
                    (sup, hdr) => sup.TestEvent -= hdr,
                    (s, e) => reciever          += 1);

                supplier.Fire();
                weak = new WeakReference <IDisposable>(d, true);
            };

            // execute local method.
            exec();
            reciever.Is(1);

            supplier.Fire();
            reciever.Is(2);

            // Force GC.
            GC.Collect(0, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();
            GC.Collect(0, GCCollectionMode.Forced); // just to be sure
            System.Threading.Tasks.Task.Delay(100).Wait();

            supplier.Fire();

            // 'reciever' should stay '2', because subscription should be automatically released.
            reciever.Is(2);
        }
コード例 #2
0
        public void NormalRelease()
        {
            var supplier = new OnDemandEvent <int>();
            int reciever = 0;
            var dispo    = supplier.WeakSubscribe <OnDemandEvent <int> >(
                (sup, hdr) => sup.TestEvent += hdr,
                (sup, hdr) => sup.TestEvent -= hdr,
                (s, e) => { reciever += 1; });

            supplier.Fire();
            reciever.Is(1);

            dispo.Dispose();
            supplier.Fire();
            reciever.Is(1);
        }
コード例 #3
0
        public void NormalRelease()
        {
            var    supplier = new OnDemandEvent <int>();
            int    reciever = int.MinValue;
            string sender   = null;
            var    dispo    = supplier.WeakSubscribe <OnDemandEvent <int>, int>(
                (sup, hdr) => sup.GenericTestEvent += hdr,
                (sup, hdr) => sup.GenericTestEvent -= hdr,
                (s, e) => { reciever = e; sender = s.GetType().Name; });

            supplier.Fire(1);
            reciever.Is(1);

            dispo.Dispose();
            supplier.Fire(3);
            reciever.Is(1);
        }
コード例 #4
0
        public void NormalRelease()
        {
            var    supplier = new OnDemandEvent <int>();
            string reciever = null;

            var dispo = supplier.WeakSubscribe <OnDemandEvent <int>, int, Action <string> >(
                f => (s, e) => f(e.ToString()),
                (sup, hdr) => sup.GenericTestEvent += hdr,
                (sup, hdr) => sup.GenericTestEvent -= hdr,
                e => { reciever = e; });

            supplier.Fire(1);
            reciever.Is(1.ToString());

            dispo.Dispose();
            supplier.Fire(3);
            reciever.Is(1.ToString());
        }
コード例 #5
0
        public void NoConvert()
        {
            var supplier = new OnDemandEvent <int>();
            int reciever = int.MinValue;

            var dispo = supplier.WeakSubscribe <OnDemandEvent <int>, int, EventHandler <int> >(
                f => (s, e) => f(s, e),
                (sup, hdr) => sup.GenericTestEvent += hdr,
                (sup, hdr) => sup.GenericTestEvent -= hdr,
                (s, e) => { reciever = e; });

            supplier.Fire(1);
            reciever.Is(1);

            dispo.Dispose();
            supplier.Fire(3);
            reciever.Is(1);
        }
コード例 #6
0
        public void NoConvert()
        {
            var supplier = new OnDemandEvent <int>();
            int reciever = 0;

            var dispo = supplier.WeakSubscribe <OnDemandEvent <int>, EventHandler>(
                f => (s, e) => f(s, e),
                (sup, hdr) => sup.TestEvent += hdr,
                (sup, hdr) => sup.TestEvent -= hdr,
                (s, e) => { reciever += 1; });

            supplier.Fire();
            reciever.Is(1);

            dispo.Dispose();
            supplier.Fire();
            reciever.Is(1);
        }
コード例 #7
0
        public void NormalRelease()
        {
            var    supplier = new OnDemandEvent <int>();
            string reciever = null;
            int    count    = 0;

            var dispo = supplier.WeakSubscribe <OnDemandEvent <int>, Action <string> >(
                f => (s, e) => f($"{++count}"),
                (sup, hdr) => sup.TestEvent += hdr,
                (sup, hdr) => sup.TestEvent -= hdr,
                e => { reciever = e; });

            supplier.Fire();
            reciever.Is(1.ToString());

            dispo.Dispose();
            supplier.Fire();
            reciever.Is(1.ToString());
        }