コード例 #1
0
        public void MultipleThreads()
        {
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue      = dispatcher.CreateQueue();

            Thread thread1 = new Thread(new ThreadStart(() =>
            {
                ThreadWorkerA(queue);
            }));

            Thread thread2 = new Thread(new ThreadStart(() =>
            {
                ThreadWorkerB(queue);
            }));

            // start the threads
            counter = 0;
            thread1.Start();
            thread2.Start();

            // spin waiting for threads to end
            while (thread1.IsAlive || thread2.IsAlive)
            {
                dispatcher.ProcessQueues();
            }
            dispatcher.ProcessQueues();

            Assert.IsTrue(counter == 30);
        }
コード例 #2
0
        public void DisposeDispatcherFirst()
        {
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue      = dispatcher.CreateQueue();

            dispatcher.Dispose();
            Assert.IsNull(queue.Dispatcher);

            ((IDisposable)queue).Dispose();
        }
コード例 #3
0
        public void DisposeQueueInsideAction()
        {
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue      = dispatcher.CreateQueue();

            queue.Enqueue(() => {});
            queue.Enqueue(() => { ((IDisposable)queue).Dispose(); });
            queue.Enqueue(() => {});

            dispatcher.ProcessQueues();
            dispatcher.Dispose();
        }
コード例 #4
0
        public void DisposeQueueFirst()
        {
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue      = dispatcher.CreateQueue();
            string           name       = queue.Name;

            ((IDisposable)queue).Dispose();
            Assert.IsNull(queue.Dispatcher);
            Assert.IsNull(dispatcher.GetQueueByName(name));

            dispatcher.Dispose();
        }
コード例 #5
0
        public void SimpleFunction()
        {
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue      = dispatcher.CreateQueue();

            counter = 0;
            for (int i = 0; i < 10; i++)
            {
                queue.Enqueue(IncrementCounter);
            }

            // all the actions should be processed
            dispatcher.ProcessQueues();
            Assert.IsTrue(counter == 10);
        }
コード例 #6
0
        public void SimpleLambda()
        {
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue      = dispatcher.CreateQueue();

            int localCounter = 0;

            for (int i = 0; i < 10; i++)
            {
                queue.Enqueue(() => { localCounter += 1; });
            }
            dispatcher.ProcessQueues();

            Assert.IsTrue(localCounter == 10);
        }
コード例 #7
0
        public void CreateMultipleQueues()
        {
            // test CreateQueue - succeeds
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue1     = dispatcher.CreateQueue();

            Assert.IsNotNull(queue1);
            Assert.IsNotNull(queue1.Name);
            Assert.IsTrue(queue1.Dispatcher == dispatcher);

            // test CreateQueue - succeeds again
            IActionQueue queue2 = dispatcher.CreateQueue();

            Assert.IsNotNull(queue2);
            Assert.IsNotNull(queue2.Name);
            Assert.IsTrue(queue2.Dispatcher == dispatcher);

            // verify the two queues are different
            Assert.AreNotSame(queue1, queue2);
            Assert.IsTrue(queue1.Name != queue2.Name);

            // test GetQueueByName - succeeds
            IActionQueue a = dispatcher.GetQueueByName(queue1.Name);

            Assert.AreSame(queue1, a);

            // test GetQueueByName - succeeds
            IActionQueue b = dispatcher.GetQueueByName(queue2.Name);

            Assert.AreSame(queue2, b);

            // test GetQueueByName - fails
            IActionQueue c = dispatcher.GetQueueByName("not_found");

            Assert.IsNull(c);
        }
コード例 #8
0
        public void NestedEnqueue()
        {
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue      = dispatcher.CreateQueue();

            counter = 0;
            for (int i = 0; i < 10; i++)
            {
                queue.Enqueue(() => {
                    counter += 1;
                    queue.Enqueue(IncrementCounter);
                });
            }

            // the first 10 actions should be processed
            dispatcher.ProcessQueues();
            Assert.IsTrue(counter == 10);

            // the secondary actions should be processed
            dispatcher.ProcessQueues();
            Assert.IsTrue(counter == 20);
        }