예제 #1
0
        public void PeriodicTimer_Elapsed_CanChangeToSingleShot()
        {
            bool autoReset = true;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        timer.SetSingleShot(timer.Interval);
                        autoReset = timer.AutoReset;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.IsFalse(autoReset, "Periodic Timer should be able to change to Single-shot within Elapsed");
        }
예제 #2
0
        public void PeriodicTimer_Elapsed_IsEnabled()
        {
            bool enabled = false;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        enabled = timer.Enabled;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.IsTrue(enabled, "Periodic Timer should be enabled when called from Elapsed");
        }
예제 #3
0
        public void PeriodicTimer_Elapsed_CanChangeInterval()
        {
            TimeSpan interval = default(TimeSpan);

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        timer.Interval = TimeSpan.FromMilliseconds(1);
                        interval       = timer.Interval;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(TimeSpan.FromMilliseconds(1), interval, "Interval should be honored when called from Elapsed");
        }
예제 #4
0
        public void PeriodicTimer_Elapsed_CanChangeInterval()
        {
            TimeSpan interval = default(TimeSpan);

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer = new Timer();
                    timer.Elapsed += () =>
                    {
                        timer.Interval = TimeSpan.FromMilliseconds(1);
                        interval = timer.Interval;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval = TimeSpan.FromMilliseconds(0);
                    timer.Enabled = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(TimeSpan.FromMilliseconds(1), interval, "Interval should be honored when called from Elapsed");
        }
예제 #5
0
        public void PeriodicTimer_Elapsed_CanCancelTimer()
        {
            int actionCount = 0;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        ++actionCount;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(1, actionCount, "Timer did not honor Cancel when called from Elapsed");
        }
예제 #6
0
        public void PeriodicTimer_Elapsed_CanCancelTimer()
        {
            int actionCount = 0;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer = new Timer();
                    timer.Elapsed += () =>
                    {
                        ++actionCount;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval = TimeSpan.FromMilliseconds(0);
                    timer.Enabled = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(1, actionCount, "Timer did not honor Cancel when called from Elapsed");
        }
예제 #7
0
        public void Timer_AfterCancel_IsDisabled()
        {
            bool enabled = true;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.SetPeriodic(TimeSpan.FromMilliseconds(50));
                        timer.Cancel();
                        enabled = timer.Enabled;
                    }
                });
            }

            Assert.IsFalse(enabled, "Timer.Enabled should be false");
        }
예제 #8
0
        public void PeriodicTimer_Elapsed_CanChangeToSingleShot()
        {
            bool autoReset = true;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer = new Timer();
                    timer.Elapsed += () =>
                    {
                        timer.SetSingleShot(timer.Interval);
                        autoReset = timer.AutoReset;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval = TimeSpan.FromMilliseconds(0);
                    timer.Enabled = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.IsFalse(autoReset, "Periodic Timer should be able to change to Single-shot within Elapsed");
        }
예제 #9
0
        public void Timer_AfterCancel_IsDisabled()
        {
            bool enabled = true;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.SetPeriodic(TimeSpan.FromMilliseconds(50));
                        timer.Cancel();
                        enabled = timer.Enabled;
                    }
                });
            }

            Assert.IsFalse(enabled, "Timer.Enabled should be false");
        }
예제 #10
0
        public void PeriodicTimer_Elapsed_IsEnabled()
        {
            bool enabled = false;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer = new Timer();
                    timer.Elapsed += () =>
                    {
                        enabled = timer.Enabled;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval = TimeSpan.FromMilliseconds(0);
                    timer.Enabled = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.IsTrue(enabled, "Periodic Timer should be enabled when called from Elapsed");
        }