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