Exemple #1
0
        public void DispatcherInvokeBasicTest()
        {
            TestTaskScheduler scheduler = (TestTaskScheduler)ApplicationHost.Current.TaskScheduler;

            using (scheduler.DisableImmediateProcessing())
            {
                Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

                int index = 1;
                int task1 = 0;
                int task2 = 0;
                int task3 = 0;

                dispatcher.InvokeAsync(() => task3 = index++, (DispatcherPriority)1);
                dispatcher.InvokeAsync(() => task1 = index++, (DispatcherPriority)3);
                dispatcher.Invoke(() => task2      = index++, (DispatcherPriority)2);

                Assert.AreEqual(1, task1);
                Assert.AreEqual(2, task2);
                Assert.AreEqual(0, task3);

                scheduler.ProcessDueOperations();

                Assert.AreEqual(1, task1);
                Assert.AreEqual(2, task2);
                Assert.AreEqual(3, task3);
            }
        }
Exemple #2
0
        public void DispatcherDisableProcessingTest()
        {
            TestTaskScheduler scheduler = (TestTaskScheduler)ApplicationHost.Current.TaskScheduler;

            using (scheduler.DisableImmediateProcessing())
            {
                Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

                int         index = 1;
                int         task1 = 0;
                int         task2 = 0;
                IDisposable dispatcherProcessingDisabled = null;

                dispatcher.InvokeAsync(() =>
                {
                    task1 = index++;
                    dispatcherProcessingDisabled = dispatcher.DisableProcessing();
                });

                dispatcher.InvokeAsync(() => task2 = index++);

                Assert.AreEqual(0, task1);
                Assert.AreEqual(0, task2);
                Assert.AreEqual(null, dispatcherProcessingDisabled);

                scheduler.ProcessDueOperations();
                Assert.AreEqual(1, task1);
                Assert.AreEqual(0, task2);
                Assert.AreNotEqual(null, dispatcherProcessingDisabled);

                dispatcherProcessingDisabled.Dispose();
                Assert.AreEqual(1, task1);
                Assert.AreEqual(0, task2);

                scheduler.ProcessDueOperations();
                Assert.AreEqual(1, task1);
                Assert.AreEqual(2, task2);
            }
        }