예제 #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 DisposeQueueInsideAction()
        {
            ManualDispatcher dispatcher = new ManualDispatcher();
            IActionQueue     queue      = dispatcher.CreateQueue();

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

            dispatcher.ProcessQueues();
            dispatcher.Dispose();
        }
예제 #3
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);
        }
예제 #4
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);
        }
예제 #5
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);
        }