public void Execute()
        {
            DateTime currentTime = DateTime.Now;

            if (!started)
            {
                started   = true;
                startTime = currentTime;
                engine.AddCommand(this);
            }
            else
            {
                // When a thread in a multithreaded program waits for an event, the thread usually invokes an
                // operating system call that blocks the thread until the event has occurred.
                // This program does not block. Instead, if the event it is waiting for (elapsedTime.TotalMilliseconds<sleepTime)
                // has not occurred, the thread simply puts itself back into the ActiveObjectEngine.
                TimeSpan elapsedTime = currentTime - startTime;
                if (elapsedTime.TotalMilliseconds < sleepTime)
                {
                    engine.AddCommand(this);
                }
                else
                {
                    engine.AddCommand(wakeupCommand);
                }
            }
        }
        public void TestSleep()
        {
            WakeUpCommand      wakeup = new WakeUpCommand();
            ActiveObjectEngine e      = new ActiveObjectEngine();

            // Building multithreaded systems using variations of this technique has been, and will continue to be, a
            // very common practice.Threads of this kind have been known as run - to - completion tasks(RTC);
            // each Command instance runs to completion before the next Command instance can run.The name RTC
            // implies that the Command instances do not block.

            // The fact that the Command instances all run to completion gives RTC threads the interesting advantage
            // that they all share the same runtime stack. Unlike the threads in a traditional multithreaded system,
            // it is not necessary to define or allocate a separate runtime stack for each RTC thread.This can be a
            // powerful advantage in memory - constrained systems with many threads.


            SleepCommand c = new SleepCommand(1000, e, wakeup);

            e.AddCommand(c);
            DateTime start = DateTime.Now;

            e.Run();
            DateTime stop      = DateTime.Now;
            double   sleepTime = (stop - start).TotalMilliseconds;

            Assert.IsTrue(sleepTime >= 1000, "SleepTime " + sleepTime + " expected > 1000");
            Assert.IsTrue(sleepTime <= 1100, "SleepTime " + sleepTime + " expected < 1100");
            Assert.IsTrue(wakeup.executed, "Command Executed");
        }
        public static void Test()
        {
            // each Command instance runs to completion before the next Command instance can run.
            engine.AddCommand(new DelayedTyper(100, '1'));
            engine.AddCommand(new DelayedTyper(300, '3'));
            engine.AddCommand(new DelayedTyper(500, '5'));
            engine.AddCommand(new DelayedTyper(700, '7'));

            Command stopCommand = new StopCommand();

            engine.AddCommand(new SleepCommand(20000, engine, stopCommand));
            engine.Run();
        }