コード例 #1
0
        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");
        }
コード例 #2
0
        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();
        }