コード例 #1
0
        public void TestInvocationBehaviourIgnore()
        {
            OcDispatcher dispatcher = new OcDispatcher();

            dispatcher.NewInvocationBehaviour = NewInvocationBehaviour.Cancel;
            bool called = false;

            dispatcher.Invoke(() => { called = true; });
            dispatcher.Invoke(s => { called = true; }, new object());
            dispatcher.InvokeAsync(() => { called = true; });
            dispatcher.InvokeAsync(s => { called = true; }, new object());

            dispatcher.Invoke(() => { called = true; return(0); });
            dispatcher.Invoke(s => { called = true; return(0); }, new object());
            dispatcher.InvokeAsync(() => { called = true; return(0); });
            dispatcher.InvokeAsync(s => { called = true; return(0); }, new object());

            dispatcher.InvokeAsyncAwaitable(() => { called = true; });
            dispatcher.InvokeAsyncAwaitable(s => { called = true; }, new object());
            dispatcher.InvokeAsyncAwaitable(() => { called = true; return(0); });
            dispatcher.InvokeAsyncAwaitable(s => { called = true; return(0); }, new object());


            dispatcher.NewInvocationBehaviour = NewInvocationBehaviour.Accept;
            dispatcher.Pass();

            Assert.IsFalse(called);
            dispatcher.Dispose();
        }
コード例 #2
0
        public void TestInvokeAsyncAwaitableFuncStateDispatcherThread()
        {
            OcDispatcher         dispatcher = new OcDispatcher(2);
            int                  count      = 0;
            ManualResetEventSlim mres       = new ManualResetEventSlim(false);
            object               context    = new object();

            dispatcher.Invoke(() =>
            {
                Invocation invocation = dispatcher.CurrentInvocation;

                dispatcher.InvokeAsync(async() =>
                {
                    count++;
                    Assert.AreEqual(dispatcher.CurrentInvocation.Parent, invocation);
                    Assert.IsTrue(dispatcher.CurrentInvocation.SetSynchronizationContext);

                    int returnCount = await dispatcher.InvokeAsyncAwaitable(state =>
                    {
                        Assert.AreEqual(count, 1);
                        count++;
                        Assert.AreEqual(dispatcher.CurrentInvocation.Priority, 0);
                        Assert.AreEqual(dispatcher.CurrentInvocation.Context, null);
                        return(count);
                    }, new object());

                    Assert.AreEqual(dispatcher.CurrentInvocation.Parent, invocation);
                    Assert.AreEqual(returnCount, 2);
                    Assert.AreEqual(count, 2);
                    count++;
                    Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, dispatcher.ManagedThreadId);
                    Assert.AreEqual(dispatcher.CurrentInvocation.Priority, 1);
                    Assert.AreEqual(dispatcher.CurrentInvocation.Context, context);
                    mres.Set();
                }, 1, context, true);
            });


            mres.Wait();
            mres.Dispose();
            Assert.AreEqual(count, 3);
            dispatcher.Dispose();
        }
コード例 #3
0
        public void TestInvokeAsyncAwaitableFuncStateNonDispatcherThread()
        {
            TestSynchronizationConext testSynchronizationConext = new TestSynchronizationConext();

            SynchronizationContext.SetSynchronizationContext(testSynchronizationConext);

            OcDispatcher         dispatcher = new OcDispatcher(2);
            int                  count      = 0;
            ManualResetEventSlim mres       = new ManualResetEventSlim(false);
            object               context    = new object();

            async void test()
            {
                count++;

                int count1 = await dispatcher.InvokeAsyncAwaitable(state =>
                {
                    Assert.AreEqual(count, 1);
                    count++;
                    Assert.AreEqual(dispatcher.CurrentInvocation.Priority, 1);
                    Assert.AreEqual(dispatcher.CurrentInvocation.Context, context);
                    return(count);
                }, new object(), 1, context);

                Assert.AreEqual(count1, 2);
                Assert.AreEqual(count, 2);
                count++;
                Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, dispatcher.ManagedThreadId);
                Assert.AreEqual(dispatcher.CurrentInvocation, null);
                mres.Set();
            }

            test();
            mres.Wait();
            mres.Dispose();
            Assert.AreEqual(count, 3);
            dispatcher.Dispose();
        }