Example #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");
        }
Example #2
0
        static void timer_Elapsed()
        {
            Log.Debug("Timer fired");
            int timeout = 0;

            if (Session.GetSessionPtr() != null)
            {
                do
                {
                    libspotify.sp_session_process_events(Session.GetSessionPtr(), out timeout);
                } while (timeout == 0);
                timer.SetSingleShot(TimeSpan.FromMilliseconds(timeout > 0 ? timeout : 1000));
                timer.Enabled = true;
            }
        }
Example #3
0
        public void TimerInterval_AfterSetSingleShot_IsSetToArgument()
        {
            TimeSpan interval = default(TimeSpan);

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.SetSingleShot(TimeSpan.FromMilliseconds(50));
                        interval = timer.Interval;
                    }
                });
            }

            Assert.AreEqual(TimeSpan.FromMilliseconds(50), interval, "Timer should have its Interval set");
        }
Example #4
0
        public void TimerType_AfterSetSingleShot_IsSingleShot()
        {
            bool autoReset = true;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.SetSingleShot(TimeSpan.FromMilliseconds(50));
                        autoReset = timer.AutoReset;
                    }
                });
            }

            Assert.IsFalse(autoReset, "Timer should be single-shot");
        }
Example #5
0
        public void Timer_AfterSetSingleShot_IsEnabled()
        {
            bool enabled = false;

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

            Assert.IsTrue(enabled, "Timer.Enabled should be true");
        }
Example #6
0
        public void Timer_Running_CanChangeInterval()
        {
            TimeSpan interval = default(TimeSpan);

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.SetSingleShot(TimeSpan.FromMilliseconds(0));
                        timer.Interval = TimeSpan.FromMilliseconds(1);
                        interval       = timer.Interval;
                    }
                });
            }

            Assert.AreEqual(TimeSpan.FromMilliseconds(1), interval, "Interval should be settable while the timer is running");
        }
Example #7
0
        public void Timer_ElapsedAfterDisposed_DoesNotInvokeElapsed()
        {
            bool sawAction = false;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.Elapsed += () => { sawAction = true; };
                        timer.SetSingleShot(TimeSpan.FromMilliseconds(0));
                        Thread.Sleep(10);
                    }
                });
            }

            Assert.IsFalse(sawAction, "Disposed timer invoked Elapsed");
        }
    // This is the first action done by the main loop
    static void FirstAction()
    {
        // Create the timer
        Timer timer = new Timer();

        // Set the Elapsed handler
        timer.Elapsed += timer_Elapsed;

        // Start the timer
        timer.SetSingleShot(TimeSpan.FromMilliseconds(1));
        Thread.Sleep(10);

        // Dispose the timer
        timer.Dispose();

        // Note that the elapsed method will *not* be called
        //  even though the timer has elapsed, because it was
        //  Disposed before returning to the main loop

        // Exit the main loop
        ActionDispatcher.Current.QueueExit();
    }
Example #9
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");
        }
Example #10
0
        public void Timer_Running_CanChangeInterval()
        {
            TimeSpan interval = default(TimeSpan);

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.SetSingleShot(TimeSpan.FromMilliseconds(0));
                        timer.Interval = TimeSpan.FromMilliseconds(1);
                        interval = timer.Interval;
                    }
                });
            }

            Assert.AreEqual(TimeSpan.FromMilliseconds(1), interval, "Interval should be settable while the timer is running");
        }
Example #11
0
        public void Timer_ElapsedAfterDisposed_DoesNotInvokeElapsed()
        {
            bool sawAction = false;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.Elapsed += () => { sawAction = true; };
                        timer.SetSingleShot(TimeSpan.FromMilliseconds(0));
                        Thread.Sleep(10);
                    }
                });
            }

            Assert.IsFalse(sawAction, "Disposed timer invoked Elapsed");
        }
Example #12
0
        public void Timer_AfterSetSingleShot_IsEnabled()
        {
            bool enabled = false;

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

            Assert.IsTrue(enabled, "Timer.Enabled should be true");
        }
Example #13
0
        public void TimerType_AfterSetSingleShot_IsSingleShot()
        {
            bool autoReset = true;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.SetSingleShot(TimeSpan.FromMilliseconds(50));
                        autoReset = timer.AutoReset;
                    }
                });
            }

            Assert.IsFalse(autoReset, "Timer should be single-shot");
        }
Example #14
0
        public void TimerInterval_AfterSetSingleShot_IsSetToArgument()
        {
            TimeSpan interval = default(TimeSpan);

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    using (Timer timer = new Timer())
                    {
                        timer.SetSingleShot(TimeSpan.FromMilliseconds(50));
                        interval = timer.Interval;
                    }
                });
            }

            Assert.AreEqual(TimeSpan.FromMilliseconds(50), interval, "Timer should have its Interval set");
        }