public void TestPostBeforeStartInCurrentThread()
        {
            using (MessageQueue queue = new MessageQueue())
            {
                object received = null;
                queue.MessageReceived += delegate(object sender, MessageQueueEventArgs e)
                {
                    received = e.Message;
                    queue.Terminate();
                };

                object message = new object();
                queue.Post(message);
                queue.StartInCurrentThread();

                Assert.AreSame(message, received);
            }
        }
        public void TestPostBeforeStartInCurrentThread()
        {
            using (MessageQueue queue = new MessageQueue())
            {
                object received = null;
                queue.MessageReceived += delegate(object sender, MessageQueueEventArgs e)
                {
                    received = e.Message;
                    queue.Terminate();
                };

                object message = new object();
                queue.Post(message);
                queue.StartInCurrentThread();

                Assert.AreSame(message, received);
            }
        }
        public void TestContinueQueueAfterTerminate()
        {
            using (MessageQueue queue = new MessageQueue())
            {
                object received = null;
                queue.MessageReceived += delegate(object sender, MessageQueueEventArgs e)
                {
                    received = e.Message;
                };

                queue.StartInAnotherThread();

                // Keep the queue busy for 100 ms
                queue.Post(x => Thread.Sleep(100), null);

                // Post a message that will be quickly handled as soon as the above sleep finishes
                object message1 = new object();
                queue.Post(message1);

                // Post a termination request to the queue.
                queue.Terminate();

                // Post a second message - because this is after the Terminate it will not be
                // processed until the queue is restarted.
                object message2 = new object();
                queue.Post(message2);

                // Allow time for the messages processes so far (up to the terminate) to be processed.
                Thread.Sleep(500);

                // message1 should have been processed, but not message2.
                Assert.AreSame(message1, received);

                // Once we restart the queue, message2 should be processed.
                queue.StartInAnotherThread();
                queue.TerminateAndWait();
                Assert.AreSame(message2, received);
            }
        }
        public void TestContinueQueueAfterTerminate()
        {
            using (MessageQueue queue = new MessageQueue())
            {
                object received = null;
                queue.MessageReceived += delegate(object sender, MessageQueueEventArgs e)
                {
                    received = e.Message;
                };

                queue.StartInAnotherThread();

                // Keep the queue busy for 100 ms
                queue.Post(x => Thread.Sleep(100), null);

                // Post a message that will be quickly handled as soon as the above sleep finishes
                object message1 = new object();
                queue.Post(message1);

                // Post a termination request to the queue.
                queue.Terminate();

                // Post a second message - because this is after the Terminate it will not be
                // processed until the queue is restarted.
                object message2 = new object();
                queue.Post(message2);

                // Allow time for the messages processes so far (up to the terminate) to be processed.
                Thread.Sleep(500);

                // message1 should have been processed, but not message2.
                Assert.AreSame(message1, received);

                // Once we restart the queue, message2 should be processed.
                queue.StartInAnotherThread();
                queue.TerminateAndWait();
                Assert.AreSame(message2, received);
            }
        }