Exemplo n.º 1
0
        public void WeakHandlerTest()
        {
#if DEBUG
            Assert.Warn("This test case should run on release build.");
            return;
#endif
            using (var dispatcher = new TestDispatcherContext())
            {
                int eventReceivedCount = 0;

                DispatcherHelper.UIDispatcher = dispatcher.Dispatcher;

                var handlerResultList = new List <string>();

                var command = new ViewModelCommand(() => { }, () => true);

                var realReceiver1 = new EventReceiver(command, () => eventReceivedCount++);
                var realReceiver2 = new EventReceiver(command, () => eventReceivedCount++);

                var receiver1 = new WeakReference(realReceiver1);
                var receiver2 = new WeakReference(realReceiver2);
                var receiver3 = new EventReceiver(command, () => eventReceivedCount++);

                var semaphore       = new Semaphore(0, 1);
                var releaseReceiver = new EventReceiver(command, () => semaphore.Release());

                command.RaiseCanExecuteChanged();
                semaphore.WaitOne(1000).Is(true);

                eventReceivedCount.Is(3);

                realReceiver1 = null;
                realReceiver2 = null;

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

                receiver1.IsAlive.Is(false);
                receiver2.IsAlive.Is(false);
                receiver3.IsNot(null);       // do not remove this line. this line is for no released receiver3 by GC.
                releaseReceiver.IsNot(null); // do not remove this line. this line is for no released releaseReceiver by GC.

                command.RaiseCanExecuteChanged();
                semaphore.WaitOne(1000).Is(true);
                eventReceivedCount.Is(4);
            }
        }
Exemplo n.º 2
0
        public void WeakHandlerTest()
        {
            using (var dispatcher = new TestDispatcherContext())
            {
                int eventReceivedCount = 0;

                DispatcherHelper.UIDispatcher = dispatcher.Dispatcher;

                var handlerResultList = new List <string>();

                var command = new ViewModelCommand(() => { }, () => true);

                var realReceiver1 = new EventReceiver(command, () => eventReceivedCount++);
                var realReceiver2 = new EventReceiver(command, () => eventReceivedCount++);

                var receiver1 = new WeakReference(realReceiver1);
                var receiver2 = new WeakReference(realReceiver2);
                var receiver3 = new EventReceiver(command, () => eventReceivedCount++);

                command.RaiseCanExecuteChanged();
                System.Threading.Thread.Sleep(1000);

                eventReceivedCount.Is(3);

                realReceiver1 = null;
                realReceiver2 = null;

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

                receiver1.IsAlive.Is(false);
                receiver2.IsAlive.Is(false);
                receiver3.IsNot(null);

                command.RaiseCanExecuteChanged();
                System.Threading.Thread.Sleep(1000);
                eventReceivedCount.Is(4);
            }
        }
Exemplo n.º 3
0
        public void WeakHandlerTest()
        {
            using (var dispatcher = new TestDispatcherContext())
            {
                int eventReceivedCount = 0;

                DispatcherHelper.UIDispatcher = dispatcher.Dispatcher;

                var handlerResultList = new List<string>();

                var command = new ViewModelCommand(() => { }, () => true);

                var realReceiver1 = new EventReceiver(command,() => eventReceivedCount++);
                var realReceiver2 = new EventReceiver(command, () => eventReceivedCount++);

                var receiver1 = new WeakReference(realReceiver1);
                var receiver2 = new WeakReference(realReceiver2);
                var receiver3 = new EventReceiver(command, () => eventReceivedCount++);

                command.RaiseCanExecuteChanged();
                System.Threading.Thread.Sleep(1000);

                eventReceivedCount.Is(3);

                realReceiver1 = null;
                realReceiver2 = null;

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

                receiver1.IsAlive.Is(false);
                receiver2.IsAlive.Is(false);
                receiver3.IsNot(null);

                command.RaiseCanExecuteChanged();
                System.Threading.Thread.Sleep(1000);
                eventReceivedCount.Is(4);
            }
        }